[2.0] Changes to <xsl:number> in XSLT 2.0

There are some changes to the <xsl:number> element in XSLT 2.0. First of all, three new formats have been added: w, W, and Ww. Those formats produce words in the default language on your machine (there’s also a lang attribute you can use to change the language). Here’s an example:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:text>Automobile manufacturers and their cars&#xA;</xsl:text>
    <xsl:for-each select="cars/manufacturer">
      <xsl:value-of select="@name"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="car">
        <xsl:text>  Car </xsl:text>
        <xsl:number count="car" level="single" format="w"/>
        <xsl:text>: </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

This stylesheet produces this text:

Automobile manufacturers and their cars
Chevrolet
  Car one: Cavalier
  Car two: Corvette
  Car three: Impala
  Car four: Malibu
Ford
  Car one: Pinto
  Car two: Mustang
  Car three: Taurus
Volkswagen
  Car one: Beetle
  Car two: Jetta
  Car three: Passat
  Car four: Touraeg

Using the format W produces the numbers ONE, TWO, THREE, and so forth, while the format Ww produces One, Two, Three.

Another difference is the addition of the select attribute. Normally <xsl:number> numbers the current node; you can use the select attribute to generate the number for another node.

XSLT 2.0 ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.