Breaking an XML file into smaller XML files...

Questions about XML that are not covered by the other forums should go here.
paulb
Posts: 1
Joined: Wed Jul 28, 2010 5:10 pm

Breaking an XML file into smaller XML files...

Post by paulb » Wed Jul 28, 2010 5:24 pm

Hello all!
I have an XML file I have imported from MS Excel. I now need to break the document up into smaller XML documents, each uniquely named using an element value. My guess is I will have to dust off the XSLT textbooks, but I was hoping there was a quicker way to do this in Oxygen than me having to write the transformation. Suggestions for other ways to go about this problem are welcome!

The parent XML file...

Code: Select all


    <row>
<dc:title>paul</dc:title>
<dc:creator>mom</dc:creator>
<dc:identifier>1977</dc:identifier>
</row>
<row>
<dc:title>brad</dc:title>
<dc:creator>dad</dc:creator>
<dc:identifier>1978</dc:identifier>
</row>
<row>
<dc:title>melissa</dc:title>
<dc:creator>mom and dad</dc:creator>
<dc:identifier>1979</dc:identifier>
</row>
Needs to be broken up into 3 separate XML files...
This file should be named 1977.xml

Code: Select all


    <row>
<dc:title>paul</dc:title>
<dc:creator>mom</dc:creator>
<dc:identifier>1977</dc:identifier>
</row>
And this file should be named 1978.xml

Code: Select all


    <row>
<dc:title>brad</dc:title>
<dc:creator>dad</dc:creator>
<dc:identifier>1978</dc:identifier>
</row>

Cheers, Paul

adrian
Posts: 2659
Joined: Tue May 17, 2005 4:01 pm

Re: Breaking an XML file into smaller XML files...

Post by adrian » Thu Jul 29, 2010 10:40 am

Hello,

Here's no quicker way than a stylesheet.

Try with this one:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:dc="myns">
<xsl:template match="/">
<xsl:for-each-group select="//row" group-by="dc:identifier/text()">
<xsl:result-document href="{concat(current-grouping-key(), '.xml')}" method="xml">
<xsl:element name="root">
<xsl:copy-of select="current-group()"/>
</xsl:element>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
You may need to make some small adjustments to the root element name and the namespace associated with the dc prefix.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Post Reply