Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2012, 06:45 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

For the output layout, just use

Code:
<xsl:output indent="yes"/>
Could I recommend reading my book?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 20th, 2012, 06:45 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i took care of it after posting it. Sorry :(

<xsl:output omit-xml-declaration="no" indent="yes"/>
 
Old March 23rd, 2012, 09:17 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike, i have one more requirement on how many CARRIER i am writing. The target system is not liking sending too many files. The requirement is put 50 CARRIER tags in one file. can you let me know how this can be done easy. I am thinking of using variable with counter and increase at the end of each loop and if it hits the max number, it will be set to 0. Is that a good approach?

Here is the final code i am using currently

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes"/>
	<xsl:template match="/">
		<xsl:for-each select="receipt/prod/carrier">
			<xsl:variable name="id">
				<xsl:value-of select="tokenize(document-uri(/), '/')[last()]"/>
			</xsl:variable>
			<xsl:result-document method="xml" href="file:///C:/Users/chillhx/Desktop/Phv/0322/output/{$id}_{position()}.xml">
				<receipt>
					<manufacturer>
						<xsl:value-of select="/receipt/manufacturer"/>
					</manufacturer>
					<manufacturer_gln>
						<xsl:value-of select="/receipt/manufacturer_gln"/>
					</manufacturer_gln>
					<transfer_recipt>
						<xsl:value-of select="/receipt/transfer_recipt"/>
					</transfer_recipt>
					<prod>
						<product_GTIN>
							<xsl:value-of select="/receipt/prod/product_GTIN"/>
						</product_GTIN>
						<product_LOT>
							<xsl:value-of select="/receipt/prod/product_LOT"/>
						</product_LOT>
						<production_date>
							<xsl:value-of select="/receipt/prod/production_date"/>
						</production_date>
						<expire_date>
							<xsl:value-of select="/receipt/prod/expire_date"/>
						</expire_date>
						<xsl:copy-of select="../@* | ."/>
					</prod>
				</receipt>
			</xsl:result-document>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
 
Old March 23rd, 2012, 09:50 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you use XSLT 2.0 and want to put a certain amount of elements into one result document then use an approach like
Code:
<xsl:param name="size" select="50"/> 

    <xsl:template match="/">
        <xsl:for-each-group select="receipt/prod/carrier" group-by="(position() - 1) idiv $size">
            <xsl:variable name="id" select="tokenize(document-uri(/), '/')[last()]"/>
 
            <xsl:result-document method="xml" href="file:///C:/Users/chillhx/Desktop/Phv/0322/output/{$id}_{position()}.xml">
                <result>
                   <xsl:for-each-group select="current-group()">
                      <!-- put code here to output details of each carrier element -->
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 23rd, 2012, 10:07 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the example

I am getting the following error

XTSE1080: Exactly one of the attributes group-by, group-adjacent, group-starting-with, and group-ending-with must be specified


Do i need to declare $div as well, if yes with what value?
 
Old March 23rd, 2012, 10:13 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I am sorry, the inner "for-each-group" in the code sample I posted should simply be "for-each"
Code:
<xsl:param name="size" select="50"/> 

    <xsl:template match="/">
        <xsl:for-each-group select="receipt/prod/carrier" group-by="(position() - 1) idiv $size">
            <xsl:variable name="id" select="tokenize(document-uri(/), '/')[last()]"/>
 
            <xsl:result-document method="xml" href="file:///C:/Users/chillhx/Desktop/Phv/0322/output/{$id}_{position()}.xml">
                <result>
                   <xsl:for-each select="current-group()">
                      <!-- put code here to output details of each carrier element -->
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 23rd, 2012, 10:38 AM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, that works.

Is it simple to convert this into XSLT 1.0?

Also for the following, i am not getting the file name correct, the file name comes as this "PV_SER~3.XML_1". Here is the syntax i am suing

<xsl:value-of select="tokenize(document-uri(/), '/')[last()]"/>

Thanks for your help
 
Old March 23rd, 2012, 10:46 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

XSLT 1.0 does not have any feature to create several result elements so there is no way to achieve this with XSLT 1.0, unless you look for an XSLT 1.0 processor with an extension.
As for the file name problem, you might want to start a new post for it, stating clearly the code you have, the input you have, the result you want and the one you get. That makes it easier for anyone wanting to help to suggest a solution, with the current post it looks like I would have to read through lots of old posting to try to find that information.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 28th, 2013, 12:05 PM
Registered User
 
Join Date: Feb 2013
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Split large XML to Smaller XML using XSLT

If my xml looks something like this

<?xml version="1.0" encoding="UTF-8"?>
<receipt>
<manufacturer>1</manufacturer>
<manufacturer_gln/>
<transfer_recipt/>
<prod>
<product_GTIN/>
<product_LOT/>
<production_date/>
<expire_date/>
<carrier id="123">
<carrier_type>1</carrier_type>
</carrier>

<carrier id="456">
<carrier_type>1</carrier_type>
</carrier>
<person id="123">
<fn>Life</fn>
</person>
<person id="456">
<fn>Life1</fn>
</person>

</prod>
</receipt>


can i split the xml using xslt into two xml files based on checking the id attribute of carrier and person

File1.xml
<?xml version="1.0" encoding="UTF-8"?>
<receipt>
<manufacturer>1</manufacturer>
<manufacturer_gln/>
<transfer_recipt/>
<prod>
<product_GTIN/>
<product_LOT/>
<production_date/>
<expire_date/>
<carrier id="123">
<carrier_type>1</carrier_type>
</carrier>

<person id="123">
<fn>Life</fn>
</person>


</prod>
</receipt>

File2.xml

<?xml version="1.0" encoding="UTF-8"?>
<receipt>
<manufacturer>1</manufacturer>
<manufacturer_gln/>
<transfer_recipt/>
<prod>
<product_GTIN/>
<product_LOT/>
<production_date/>
<expire_date/>

<carrier id="456">
<carrier_type>1</carrier_type>
</carrier>

<person id="456">
<fn>Life1</fn>
</person>

</prod>
</receipt>
 
Old February 28th, 2013, 12:44 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please don't add a new question to an old thread. And please say whether you are using XSLT 2.0 or whether you are constrained to 1.0.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Split large XML to smalls nzvtvsky9 XSLT 1 January 13th, 2010 01:48 PM
Split xml file with result document and javax.xml.transform.Transformer. nisargmca XSLT 3 January 12th, 2010 06:26 AM
Regarding xml-html transformation of an xml string using xslt and javascript suprakash444 XSLT 1 January 12th, 2009 01:23 AM
Convert 1 big XML to multiple smaller XML victorcorey XSLT 1 March 20th, 2008 05:59 AM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.