I'd like to split one XML file into multiple smalled files using XSLT. I read that this is possible using exsl:document
. I've managed to sort-of get it to work except one minor issue — I can only seem to output one file. Here's my XML:
<People>
<Person>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Person>
<Person>
<FirstName>Jack</FirstName>
<LastName>White</LastName>
</Person>
<Person>
<FirstName>Mark</FirstName>
<LastName>Wall</LastName>
</Person>
<Person>
<FirstName>John</FirstName>
<LastName>Ding</LastName>
</Person>
<Person>
<FirstName>Cyrus</FirstName>
<LastName>Ding</LastName>
</Person>
<Person>
<FirstName>Megan</FirstName>
<LastName>Boing</LastName>
</Person>
</People>
Here's my XSLT:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
exclude-result-prefixes="exsl"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="People/Person">
<exsl:document href="{//FirstName}_{//LastName}.xml">
<People>
<xsl:copy-of select="."/>
</People>
</exsl:document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Executing this using xsltproc data.xsl data.xml
generates only one file called John_Doe.xml
. I can't find any other files.
How can I split all the Person
into the separate files?
Change
<xsl:template match="/">
<xsl:for-each select="People/Person">
<exsl:document href="{//FirstName}_{//LastName}.xml">
<People>
<xsl:copy-of select="."/>
</People>
</exsl:document>
</xsl:for-each>
</xsl:template>
to
<xsl:template match="/">
<xsl:for-each select="People/Person">
<exsl:document href="{FirstName}_{LastName}.xml">
<People>
<xsl:copy-of select="."/>
</People>
</exsl:document>
</xsl:for-each>
</xsl:template>