xsl:number (2)

  1. number_2-input.xml

  2. <?xml version="1.0"?>
  3. <document>
  4.   <h1>Golf</h1>
  5.   <h2>Alpha</h2>
  6.   <h3>Charlie</h3>
  7.   <h3>India</h3>
  8.   <h2>Foxtrot</h2>
  9.   <h2>Zulu</h2>
  10.   <h3>Kilo</h3>
  11. </document>
  1. number_2-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="@*|node()">
  6.     <xsl:copy>
  7.       <xsl:apply-templates select="@*|node()"/>
  8.     </xsl:copy>
  9.   </xsl:template>
  10.   <xsl:template match="h2">
  11.     <xsl:copy>
  12.       <xsl:number count="h2" format="1. " />  
  13.       <xsl:apply-templates select="@*|node()"/>
  14.     </xsl:copy>
  15.   </xsl:template>
  16.   <xsl:template match="h3">
  17.     <xsl:copy>
  18.       <xsl:number count="h2" level="any" format="1." />
  19.       <xsl:number count="h3" from="h2" level="any" format="1 " />
  20.       <xsl:apply-templates select="@*|node()"/>
  21.     </xsl:copy>
  22.   </xsl:template>
  23. </xsl:stylesheet>
  1. number_2-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <document>
  4.   <h1>Alpha</h1>
  5.   <h2>1. Golf</h2>
  6.   <h3>1.1 Charlie</h3>
  7.   <h3>1.2 India</h3>
  8.   <h2>2. Foxtrot</h2>
  9.   <h2>3. Zulu</h2>
  10.   <h3>3.1 Kilo</h3>
  11. </document>

Comment

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

Updated 2009-03-19