XML technologies / XSLT / XSLT Kochbuch / From XML to XML / Splitting documents
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You want to split elements from a single document into separate documents.
Solution
XSLT 1.0
For XSLT 1.0, you must use a widely available but non-standard extension allowing multiple output documents. (Note: In XSLT 2.0 this capability is available through a new element named xsl:result-document; see also Making the most of XSLT 2.0.) The solution identifies the level in the document structure to serialize and determines the resulting filename. The following stylesheet splits the file salesBySalesPerson.xml from Selecting and traversing so that a separate file is created for each salesperson. The stylesheet works in Saxon. Saxon permits the XSLT 1.1 element xsl:document when the stylesheet version is set to 1.1; some processors also support exslt:document from exslt.org. (XSLT 1.1 is no longer the official version; it was superseded by XSLT 2.0.)
If you prefer not to use version 1.1, you can use the extension saxon:output:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="salesperson">
<xsl:variable name="outFile" select="concat('salesperson.',translate(@name,' ','_'),'.xml')"/>
<!-- Nicht-standardisiertes Saxon xsl:document! -->
<xsl:document href="{$outFile}">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:document>
</xsl:template>
<xsl:template match="salesBySalesperson">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>Discussion
Although the stylesheet shown is specific to Saxon, the technique works with most XSLT 1.0 processors after minor changes. Saxon also has the extension element saxon:output (xmlns:saxon=http://icl.com/saxon). Xalan uses xalan:redirect (xmlns:xalan=http://xml.apache.org/xalan).
An interesting variant creates an output file that includes the generated files using xinclude:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="copy.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="salesperson">
<xsl:variable name="outFile" select="concat('salesperson.',translate(@name,' ','_'),'.xml')"/>
<xsl:document href="{$outFile}">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:document>
<xi:include href="{$outFile}" xmlns:xi="http://www.w3.org/2001/XInclude"/>
</xsl:template>
</xsl:stylesheet>If you are concerned that your XSLT processor may one day encounter XInclude and mistakenly try to include the very file just output, you can replace the xi:include element with xsl:element:
<xsl:element name="xi:include" xmlns:xi="http://www.w3.org/2001/XInclude">
<xsl:attribute name="href">
<xsl:value-of select="$outFile"/>
</xsl:attribute>
</xsl:element>See also
The recipe Saxon extension functions contains further examples using extensions for multiple output documents.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de