xsl:result-document

  1. result-document_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" name="Delta" price="3250" stock="4" country="Denmark"/>
  5.   <product id="p2" name="Golf" price="1000" stock="5" country="Germany"/>
  6.   <product id="p3" name="Alpha" price="1200" stock="19" country="Germany"/>
  7.   <product id="p4" name="Foxtrot" price="1500" stock="5" country="Australia"/>
  8.   <product id="p5" name="Tango" price="1225" stock="3" country="Japan"/>
  9. </products>
  1. result-document_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.   <xsl:template match="/">      
  5.     <xsl:for-each select="products/product">
  6.       <xsl:result-document method="xml" href="product_{@id}-output.xml">
  7.         <xsl:copy-of select="."/>
  8.       </xsl:result-document>
  9.     </xsl:for-each>
  10.   </xsl:template>
  11. </xsl:stylesheet>
  1. product_p1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <product id="p1" name="Delta" price="3250" stock="4" country="Denmark"/>
  1. product_p2-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <product id="p2" name="Golf" price="1000" stock="5" country="Germany"/>
  1. product_p3-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <product id="p3" name="Alpha" price="1200" stock="19" country="Germany"/>
  1. product_p4-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <product id="p4" name="Foxtrot" price="1500" stock="5" country="Australia"/>
  1. product_p5-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <product id="p5" name="Tango" price="1225" stock="3" country="Japan"/>

Comment

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