xsl:number (1)

  1. number_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <name>Alpha</name>
  5.   <name>Bravo</name>
  6.   <name>Charlie</name>
  7. </products>
  1. number_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:output indent="yes"/>
  5.   <xsl:template match="/">
  6.     <table>
  7.       <xsl:for-each select="products/name">
  8.         <tr>
  9.           <td>
  10.             <xsl:number value="position()" format="01"/>
  11.           </td>
  12.           <td>
  13.             <xsl:value-of select="."/>
  14.           </td>
  15.         </tr>
  16.       </xsl:for-each>
  17.     </table>
  18.   </xsl:template>
  19. </xsl:stylesheet>
  1. number_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <table>
  4.   <tr>
  5.     <td>01</td>
  6.     <td>Alpha</td>
  7.   </tr>
  8.   <tr>
  9.     <td>02</td>
  10.     <td>Bravo</td>
  11.   </tr>
  12.   <tr>
  13.     <td>03</td>
  14.     <td>Charlie</td>
  15.   </tr>
  16. </table>

Comment

The xsl:number element has many attributes for fine tuning. See: http://www.w3.org/TR/xslt20/#number.

Updated 2009-03-19