Extracting Subset of XML

Here should go questions about transforming XML with XSLT and FOP.
smyles
Posts: 2
Joined: Mon Mar 01, 2010 9:27 pm

Extracting Subset of XML

Post by smyles »

Hi, I've run into a problem when trying to extract a subset of a large XML based on a reference file.

- The large XML format cannot change
- The reference file can be in whatever format (i'm creating this)

What I want to do is extract the relevant pieces from the large XML file and output this in the same format in a smaller XML file.

Below is the source XML:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<database>
<record type="a">
<value name="1"/>
<value name="2"/>
<value name="3"/>
<value name="4"/>
<value name="5"/>
<value name="6"/>
<value name="7"/>
<value name="8"/>
<value name="9"/>
<value name="10"/>
</record>
<record type="b">
<value name="1"/>
<value name="2"/>
<value name="3"/>
<value name="4"/>
<value name="5"/>
<value name="6"/>
<value name="7"/>
<value name="8"/>
<value name="9"/>
<value name="10"/>
</record>
<record type="c">
<value name="1"/>
<value name="2"/>
<value name="3"/>
<value name="4"/>
<value name="5"/>
<value name="6"/>
<value name="7"/>
<value name="8"/>
<value name="9"/>
<value name="10"/>
</record>
<record type="d">
<value name="1"/>
<value name="2"/>
<value name="3"/>
<value name="4"/>
<value name="5"/>
<value name="6"/>
<value name="7"/>
<value name="8"/>
<value name="9"/>
<value name="10"/>
</record>
</database>
The reference file I have is another XML, in it I enumerate bits I need from each section:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<record>
<index type="a">
<values>
<value>1</value>
<value>2</value>
<value>3</value>
</values>
</index>
<index type="c">
<values>
<value>1</value>
<value>3</value>
<value>6</value>
<value>7</value>
</values>
</index>
<index type="d">
<values>
<value>2</value>
<value>3</value>
</values>
</index>
</record>
The following is the stylesheet I have tried so far, which is not working..

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<record>
<record-indices>
<xsl:for-each select="document('Test_Reference.xml')/record/index">
<xsl:variable name="first" select="."/>
<xsl:for-each select="//database/record">
<xsl:variable name="second" select="."/>
<xsl:if test="$first/@type=$second/@type">
<record-index type="{$second/@type}"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</record-indices>
</record>
</xsl:template>
</xsl:stylesheet>
What I've found is that if I nest the for-each with the document() function inside the the other one that I get the output I need, but I then cannot extract only the children elements I need for each of the index types, which is why I need to nest this way.

I'm pretty new to xslt so there may be a basic principal I'm missing. Any advice would be greatly appreciated.

...if there's something that I haven't explained properly just ask. I know that it makes complete sense in my head what I want to do but that might not translate to paper!!! :roll:
smyles
Posts: 2
Joined: Mon Mar 01, 2010 9:27 pm

Re: Extracting Subset of XML

Post by smyles »

Is this actually a particularly difficult issue or am I making some fundamental mistake?
Post Reply