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 19th, 2012, 04:46 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Split large XML to Smaller XML using XSLT

I tried with several examples and i am stuck from this forum. Any help is appreciated

From Below the carrier and below tags get repeated. The files very huge and we are unable to process them.

I would like to split XML file into multiple of smaller number of CARRIERs like 10.

Can some body help to split please
Source XML
Code:
<?xml version="1.0" encoding="utf-8"?>
<receipt>
	<manufacturer>1</manufacturer>
	<manufacturer_gln></manufacturer_gln>
	<transfer_recipt></transfer_recipt>
	<prod>
		<product_GTIN></product_GTIN>
		<product_LOT></product_LOT>
		<production_date></production_date>
		<expire_date></expire_date>
		<carrier>
			<carrier_type></carrier_type>
			<carrier_barcode></carrier_barcode>
			<carrier_detail>
				<carrier>
					<carrier_type></carrier_type>
					</carrier_barcode>
					<carrier_detail>
						<carrier>
							<carrier_type></carrier_type>
							<carrier_barcode></carrier_barcode>
						</carrier>
</carrier_detail>
</carrier>
</carrier_detail>
</carrier>
</prod>
</receipt>

Last edited by chilly; March 19th, 2012 at 04:56 PM..
 
Old March 19th, 2012, 05:13 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Describing your files as "very huge" is incredibly unhelpful. It's like asking people to design a bridge strong enough to carry "very heavy" trucks. Give us some numbers. I've heard people describe files as small as 10Mb as "huge".

Since your data contains nested carrier elements, you also need to provide more detail of how the file is to be split.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 19th, 2012, 05:15 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, i get more than 100MB size and i am trying to split into multiples of 2MBs or each CARRIER section into one file with header data

Here is the simple source

Code:
<receipt>
	<manufacturer>1</manufacturer>
	<manufacturer_gln></manufacturer_gln>
	<transfer_recipt></transfer_recipt>
	<prod>
		<product_GTIN></product_GTIN>
		<product_LOT></product_LOT>
		<production_date></production_date>
		<expire_date></expire_date>
		<carrier>
			<carrier_type>1</carrier_type>
		</carrier>
		<carrier>
			<carrier_type>2</carrier_type>
		</carrier>
		<carrier>
			<carrier_type>3</carrier_type>
		</carrier>
</prod>
</receipt>
output will be 3 xml files

ONE
Code:
<receipt>
	<manufacturer>1</manufacturer>
	<manufacturer_gln></manufacturer_gln>
	<transfer_recipt></transfer_recipt>
	<prod>
		<product_GTIN></product_GTIN>
		<product_LOT></product_LOT>
		<production_date></production_date>
		<expire_date></expire_date>
		<carrier>
			<carrier_type>1</carrier_type>
		</carrier>
</prod>
</receipt>
TWO
Code:
<receipt>
	<manufacturer>1</manufacturer>
	<manufacturer_gln></manufacturer_gln>
	<transfer_recipt></transfer_recipt>
	<prod>
		<product_GTIN></product_GTIN>
		<product_LOT></product_LOT>
		<production_date></production_date>
		<expire_date></expire_date>
		<carrier>
			<carrier_type>2</carrier_type>
		</carrier>
</prod>
</receipt>
THREE
Code:
<receipt>
	<manufacturer>1</manufacturer>
	<manufacturer_gln></manufacturer_gln>
	<transfer_recipt></transfer_recipt>
	<prod>
		<product_GTIN></product_GTIN>
		<product_LOT></product_LOT>
		<production_date></production_date>
		<expire_date></expire_date>
		<carrier>
			<carrier_type>3</carrier_type>
		</carrier>
</prod>
</receipt>

Last edited by chilly; March 19th, 2012 at 05:20 PM..
 
Old March 20th, 2012, 04:20 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

any advice?
 
Old March 20th, 2012, 04:37 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

To split each carrier into a separate file try this (XSLT 2.0).

Code:
<xsl:template match="/">
  <xsl:for-each select="//carrier">
     <xsl:apply-templates select="/" mode="pcopy">
        <xsl:with-param name="current-carrier" select="." tunnel="yes"/>
     </xsl:apply-templates>
  </xsl:for-each>
</xsl:template>

<xsl:template match="*" mode="pcopy">
  <xsl:copy>
     <xsl:apply-templates mode="pcopy"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="carrier" mode="pcopy">
  <xsl:param name="current-carrier" tunnel="yes"/>
  <xsl:copy-of select=".[. is $current-carrier]"/>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 20th, 2012, 05:24 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The looping seems not working. i have 10 CARRIER tags available

It is writing Transfer/receipt/prod details with CARRIER and continue with 99 times of empty transfer/receipt/prod details with out writing next CARRIER tags.
and continues to the iteration of writing next CARRIER with 99 empty one's

Also any function i could refer to write output as an xml as well

Last edited by chilly; March 20th, 2012 at 05:40 PM..
 
Old March 20th, 2012, 06:04 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I wrote the following, seems like working as expected

Except when i turn on the following syntax, its not giving any output. any idea

<xsl:result-document method="xml" href="pv_serial_{@id}.xml">


Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:template match="/">
		<xsl:for-each select="receipt/prod/carrier">
			<!--<xsl:result-document method="xml" href="pv_serial_{@id}.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>
					<carrier>
						<xsl:copy-of select="../@* | ."/>
					</carrier>
				</prod>
			</receipt>
			<!--</xsl:result-document>-->
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
 
Old March 20th, 2012, 06:27 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Usually if xsl:result-document appears to give no output, it's writing the output but not where you expect. If you're using Saxon, set the -t option to see where it's going.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 20th, 2012, 06:29 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have it working. Thanks for the input

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<xsl:for-each select="receipt/prod/carrier">
			<xsl:result-document method="xml" href="file:///C:/Users/chillhx/Desktop/Phv/pv_serial_{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>
						<carrier>
							<xsl:copy-of select="../@* | ."/>
						</carrier>
					</prod>
				</receipt>
			</xsl:result-document>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
 
Old March 20th, 2012, 06:35 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael
Rather than going through each field writing an output from <receipt> till <carrier>, is there a way we can capture everything at once

I tried this, but not working

<xsl:copy-of select="/receipt/@*" />

It looks very bad code when i try to insert line break etc for each field. please advice

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<xsl:for-each select="receipt/prod/carrier">
			<xsl:result-document method="xml" href="file:///C:/Users/chillhx/Desktop/Phv/pv_serial_{position()}.xml">
				<xsl:value-of select="'&#xA;'"/>
				<receipt>
					<xsl:value-of select="'&#xA;'"/>
					<manufacturer>
						<xsl:value-of select="'&#xA;'"/>
						<xsl:value-of select="/receipt/manufacturer"/>
						<xsl:value-of select="'&#xA;'"/>
					</manufacturer>
					<xsl:value-of select="'&#xA;'"/>
					<manufacturer_gln>
						<xsl:value-of select="'&#xA;'"/>
						<xsl:value-of select="/receipt/manufacturer_gln"/>
						<xsl:value-of select="'&#xA;'"/>
					</manufacturer_gln>
					<xsl:value-of select="'&#xA;'"/>
					<transfer_recipt>
						<xsl:value-of select="'&#xA;'"/>
						<xsl:value-of select="/receipt/transfer_recipt"/>
						<xsl:value-of select="'&#xA;'"/>
					</transfer_recipt>
					<xsl:value-of select="'&#xA;'"/>
					<prod>
						<xsl:value-of select="'&#xA;'"/>
						<product_GTIN>
							<xsl:value-of select="'&#xA;'"/>
							<xsl:value-of select="/receipt/prod/product_GTIN"/>
							<xsl:value-of select="'&#xA;'"/>
						</product_GTIN>
						<xsl:value-of select="'&#xA;'"/>
						<product_LOT>
							<xsl:value-of select="'&#xA;'"/>
							<xsl:value-of select="/receipt/prod/product_LOT"/>
							<xsl:value-of select="'&#xA;'"/>
						</product_LOT>
						<xsl:value-of select="'&#xA;'"/>
						<production_date>
							<xsl:value-of select="'&#xA;'"/>
							<xsl:value-of select="/receipt/prod/production_date"/>
							<xsl:value-of select="'&#xA;'"/>
						</production_date>
						<xsl:value-of select="'&#xA;'"/>
						<expire_date>
							<xsl:value-of select="'&#xA;'"/>
							<xsl:value-of select="/receipt/prod/expire_date"/>
							<xsl:value-of select="'&#xA;'"/>
						</expire_date>
						<xsl:value-of select="'&#xA;'"/>
						<carrier>
							<xsl:value-of select="'&#xA;'"/>
							<xsl:copy-of select="../@* | ."/>
							<xsl:value-of select="'&#xA;'"/>
						</carrier>
						<xsl:value-of select="'&#xA;'"/>
					</prod>
					<xsl:value-of select="'&#xA;'"/>
				</receipt>
				<xsl:value-of select="'&#xA;'"/>
			</xsl:result-document>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>





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.