result-document_1-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1" name="Delta" price="3250" stock="4" country="Denmark"/>
<product
id="p2" name="Golf" price="1000" stock="5" country="Germany"/>
<product
id="p3" name="Alpha" price="1200" stock="19" country="Germany"/>
<product
id="p4" name="Foxtrot" price="1500" stock="5" country="Australia"/>
<product
id="p5" name="Tango" price="1225" stock="3" country="Japan"/>
</products>
result-document_1-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template
match="/">
<xsl:for-each
select="products/product">
<xsl:result-document
method="xml" href="product_{@id}-output.xml">
<xsl:copy-of
select="."/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
product_p1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<product
id="p1" name="Delta" price="3250" stock="4" country="Denmark"/>
product_p2-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<product
id="p2" name="Golf" price="1000" stock="5" country="Germany"/>
product_p3-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<product
id="p3" name="Alpha" price="1200" stock="19" country="Germany"/>
product_p4-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<product
id="p4" name="Foxtrot" price="1500" stock="5" country="Australia"/>
product_p5-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<product
id="p5" name="Tango" price="1225" stock="3" country="Japan"/>
We can use xsl:result-document as many times as we please. Above we use the id attribute to name the output files. In other situations we could use the position() function, etc., to get a sequence of filenames.
Updated 2009-03-19