xsl:for-each (2)

  1. for-each_2-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" price="3250" stock="4"/>  
  5.   <product id="p2" price="1000" stock="5"/>
  6. </products>
  1. for-each_2-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:template match="/">
  6.     <PRODUCTS>
  7.       <xsl:for-each select="products/product">
  8.         <PRODUCT id="{@id}" price="{@price}" stock="{@stock}"/>
  9.       </xsl:for-each>
  10.     </PRODUCTS>
  11.   </xsl:template>
  12. </xsl:stylesheet>
  1. for-each_2-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <PRODUCTS>
  4.   <PRODUCT id="p1" price="3250" stock="4"/>
  5.   <PRODUCT id="p2" price="1000" stock="5"/>
  6. </PRODUCTS>

Comment

This example is exactly like "xsl-for-each (1)" except that we have given the input file another structure using attributes instead of children elements in order to prepare the ground for longer examples that can fit into one page.

Updated 2009-03-19