I'm having a hard time wrapping my head around XSLT but I heard it's possible to split an XML file into multiple files. Basically I'd like to copy all the elements up to the first file and after the last file and then add the individual file content for each output file.
Could someone give me some pointers on this if it's even possible?
Thanks,
complete.xml
<rootelem> <elem> <file attr1='1'> <content>content file 1</content> </file> <file attr2='2'> <content>content file 2</content> </file> <file attr3='3'> <content>content file 3</content> </file> </elem> </rootelem>
OUTPUT:
complete_PART1.xml
<rootelem> <elem> <file attr1='1'> <content>content file 1</content> </file> </elem> </rootelem>
complete_PART2.xml
<rootelem> <elem> <file attr2='2'> <content>content file 2</content> </file> </elem> </rootelem>
complete_PART3.xml
<rootelem> <elem> <file attr3='3'> <content>content file 3</content> </file> </elem> </rootelem>
Split large XML file in Windows (Method #1) First, click the “Add XML File(s)” button to provide the input path of the file to split, or easily drag and drop your files. Then select the tag by which the new file will be split. Next, choose after what period of tags to split into a new file.
Execute an XSLT transformation from an XML fileOpen an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document. For example, add the following line to the document prolog: <?
XSLT is commonly used to convert XML to HTML, but can also be used to transform XML documents that comply with one XML schema into documents that comply with another schema. XSLT can also be used to convert XML data into unrelated formats, like comma-delimited text or formatting languages such as troff.
This post shows you how to convert a simple XML file to CSV using XSLT. The following XSL Style Sheet (compatible with XSLT 1.0) can be used to transform the XML into CSV. It is quite generic and can easily be configured to handle different xml elements by changing the list of fields defined ar the beginning.
Responding to your comment on @Dimitre's answer...
You wrote,
<xsl:template match="/"> <xsl:for-each select="elem/file"> <xsl:result-document method="xml" href="file_{@id}-output.xml"> <xsl:copy-of select="."/> </xsl:result-document> </xsl:for-each> </xsl:template>
This doesn't quite match your XML, which has rootelem
as an outermost element, and your comment says root
as an outermost element. You probably want something like this:
<xsl:template match="/root"> <xsl:for-each select="elem/file"> <xsl:result-document method="xml" href="file_{@id}-output.xml"> <root> <xsl:copy-of select="/root/@*" /> <elem> <xsl:copy-of select="../@* | ." /> </elem> </root> </xsl:result-document> </xsl:for-each> </xsl:template>
You could get fancier, trying to use <xsl:copy>
instead of literal result elements for root and elem, but it doesn't seem worth the effort unless they're going to vary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With