XML technologies / XSLT / XSLT Cookbook / From XML to text / Numbering text output

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want sequentially numbered output.

Solution

Output can be numbered in many ways. This recipe presents increasingly complex examples covering common numbering needs and some unusual ones.

Sequentially numbering siblings

This is the simplest form of numbering. For example, the following two examples create a numbered list of people. They use xsl:number and its count attribute. You can omit count because the default behaviour counts all nodes in the current context.

Example: Stylesheet.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="person">
    <xsl:number count="*" format="1. "/>
    <xsl:value-of select="@name"/>
  </xsl:template>
</xsl:stylesheet>

Example: Output.

1. Al Zehtooney 2. Brad York 3. Charles Xavier 4. David Williams 5. Edward Ulster 6. Frank Townsend 7. Greg Sutter 8. Harry Rogers 9. John Quincy 10. Kent Peterson ...

For right-aligned numbers, use the Creating a columnar report recipe’s justify (template ) for this purpose.

Starting with a number other than one

xsl:number does not directly support starting with a number other than one or incrementing by another amount. A little arithmetic solves this, as the next two examples show.

Example: Stylesheet with nonconsecutive numbering.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="person">
    <xsl:variable name="num">
      <xsl:number count="*"/>
    </xsl:variable>
    <xsl:number value="($num - 1) * 5 + 10" format="1. "/>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Example: Output.

10. Al Zehtooney 15. Brad York 20. Charles Xavier 25. David Williams 30. Edward Ulster 35. Frank Townsend 40. Greg Sutter 45. Harry Rogers 50. John Quincy 55. Kent Peterson ...

This also works with nonnumeric output formats. The next two examples use the same technique, starting at L.

Example: Stylesheet numbering from L.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="person">
    <xsl:variable name="num">
      <xsl:number count="*"/>
    </xsl:variable>
    <xsl:number value="$num + 11" format="A. "/>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Example: Output numbering people from the letter L.

L. Al Zehtooney M. Brad York N. Charles Xavier O. David Williams P. Edward Ulster Q. Frank Townsend R. Greg Sutter S. Harry Rogers T. John Quincy U. Kent Peterson ...

Numbering elements globally

Sometimes elements need consecutive numbers regardless of context. A common case is a document containing footnotes, which can occur at any structural level but need continuous numbering. To continue our example, however, here is a document dividing people into groups and subgroups:

<people>
  <group>
    <person name="Al Zehtooney" age="33" sex="m" smoker="nein"/>
    <person name="Brad York" age="38" sex="m" smoker="ja"/>
    <person name="Charles Xavier" age="32" sex="m" smoker="nein"/>
    <person name="David Williams" age="33" sex="m" smoker="nein"/>
    <person name="Edward Ulster" age="33" sex="m" smoker="ja"/>
    <person name="Frank Townsend" age="35" sex="m" smoker="nein"/>
  </group>
  <group>
    <person name="Greg Sutter" age="40" sex="m" smoker="nein"/>
    <person name="Harry Rogers" age="37" sex="m" smoker="nein"/>
    <group>
      <person name="John Quincy" age="43" sex="m" smoker="ja"/>
      <person name="Kent Peterson" age="31" sex="m" smoker="nein"/>
      <person name="Larry Newell" age="23" sex="m" smoker="nein"/>
      <group>
        <person name="Max Milton" age="22" sex="m" smoker="nein"/>
        <person name="Norman Lamagna" age="30" sex="m" smoker="nein"/>
        <person name="Ollie Kensinton" age="44" sex="m" smoker="nein"/>
      </group>
      <person name="John Frank" age="24" sex="m" smoker="nein"/>
    </group>
    <group>
      <person name="Mary Williams" age="33" sex="w" smoker="nein"/>
      <person name="Jane Frank" age="38" sex="w" smoker="ja"/>
      <person name="Jo Peterson" age="32" sex="w" smoker="nein"/>
      <person name="Angie Frost" age="33" sex="w" smoker="nein"/>
      <person name="Betty Bates" age="33" sex="w" smoker="nein"/>
      <person name="Connie Date" age="35" sex="w" smoker="nein"/>
      <person name="Donna Finster" age="20" sex="w" smoker="nein"/>
    </group>
    <group>
      <person name="Esther Gates" age="37" sex="w" smoker="nein"/>
      <person name="Fanny Hill" age="33" sex="w" smoker="ja"/>
      <person name="Geta Iota" age="27" sex="w" smoker="nein"/>
      <person name="Hillary Johnson" age="22" sex="w" smoker="nein"/>
      <person name="Ingrid Kent" age="21" sex="w" smoker="nein"/>
      <person name="Jill Larson" age="20" sex="w" smoker="nein"/>
      <person name="Kim Mulrooney" age="41" sex="w" smoker="nein"/>
      <person name="Lisa Nevins" age="21" sex="w" smoker="nein"/>
    </group>
  </group>
</people>

The only change needed is to use xsl:number with attribute level="any". This tells the XSLT processor to consider all preceding occurrences of element person when calculating the number, as the next two examples show.

Example: Stylesheet for level="any".

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="person">
    <xsl:number count="person" level="any" format="1. "/>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Example: Output with level="any".

1. Al Zehtooney 2. Brad York 3. Charles Xavier 4. David Williams 5. Edward Ulster 6. Frank Townsend 7. Greg Sutter 8. Harry Rogers 9. John Quincy 10. Kent Peterson 11. Larry Newell
12. Max Milton 13. Norman Lamagna 14. Ollie Kensinton 15. John Frank 16. Mary Williams 17. Jane Frank 18. Jo Peterson 19. Angie Frost 20. Betty Bates 21. Connie Date 22. Donna Finster 23. Esther Gates 24. Fanny Hill 25. Geta Iota 26. Hillary Johnson 27. Ingrid Kent 28. Jill Larson 29. Kim Mulrooney 30. Lisa Nevins

Global numbering within a subcontext

Sometimes global numbering must be restricted to a subcontext. For example, number people within their top-level group while ignoring subgroups:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="people/group">
    <xsl:text>Gruppe </xsl:text>
    <xsl:number count="group"/>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
  <xsl:template match="person">
    <xsl:number count="person" level="any" from="people/group" format="1. "/>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Example: Output.

Gruppe 1
1. Al Zehtooney 2. Brad York 3. Charles Xavier 4. David Williams 5. Edward Ulster 6. Frank Townsend

Gruppe 2
1. Greg Sutter 2. Harry Rogers 3. John Quincy 4. Kent Peterson 5. Larry Newell 6. Max Milton 7. Norman Lamagna 8. Ollie Kensinton 9. John Frank 10. Mary Williams 11. Jane Frank 12. Jo Peterson 13. Angie Frost 14. Betty Bates 15. Connie Date 16. Donna Finster 17. Esther Gates 18. Fanny Hill 19. Geta Iota 20. Hillary Johnson 21. Ingrid Kent 22. Jill Larson 23. Kim Mulrooney 24. Lisa Nevins

Hierarchical numbering

Formal and legal documents often number objects by both sequence and hierarchical position. As the next example shows, xsl:number supports this through attribute level="multiple".

Example: Hierarchical numbering by group and person.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="people/group">
    <xsl:text>Gruppe </xsl:text>
    <xsl:number count="group"/>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
  <xsl:template match="person">
    <xsl:number count="group | person" level="multiple" format="1.1.1 "/>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

This example’s numbering is slightly odd, but clearly illustrates the effect of attribute count when used with level="multiple". It specifies which preceding elements contribute to hierarchical numbering. The stylesheet numbers people using group or person elements. Bard York receives 1.2 because he is the second person in group 1. Max Milton receives 2.3.4.1: he is in group 2 when counting only top-level groups, in group 3 when counting both top-level groups and second-level groups, and in group 4 when counting all three levels. Within his own group, he is the first person:

Gruppe 1
1.1 Al Zehtooney 1.2 Brad York 1.3 Charles Xavier 1.4 David Williams 1.5 Edward Ulster 1.6 Frank Townsend

Gruppe 2
2.1 Greg Sutter 2.2 Harry Rogers 2.3.1 John Quincy 2.3.2 Kent Peterson 2.3.3 Larry Newell 2.3.4.1 Max Milton 2.3.4.2 Norman Lamagna 2.3.4.3 Ollie Kensinton 2.3.5 John Frank 2.4.1 Mary Williams 2.4.2 Jane Frank 2.4.3 Jo Peterson 2.4.4 Angie Frost 2.4.5 Betty Bates 2.4.6 Connie Date 2.4.7 Donna Finster 2.5.1 Esther Gates 2.5.2 Fanny Hill 2.5.3 Geta Iota 2.5.4 Hillary Johnson 2.5.5 Ingrid Kent 2.5.6 Jill Larson 2.5.7 Kim Mulrooney 2.5.8 Lisa Nevins

Typical applications expect each level’s number to be relative to the number at the next higher level. Use multiple adjacent xsl:number elements to obtain this, as shown in the next two examples.

Example: Stylesheet for multiple nested numbering levels.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="group">
    <xsl:text>Gruppe </xsl:text>
    <xsl:number count="group" level="multiple"/>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="person">
    <xsl:number count="group" level="multiple" format="1.1.1."/>
    <xsl:number count="person" level="single" format="1 "/>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Example: Output.

Gruppe 1
1.1 Al Zehtooney 1.2 Brad York 1.3 Charles Xavier 1.4 David Williams 1.5 Edward Ulster 1.6 Frank Townsend
Gruppe 2
2.1 Greg Sutter 2.2 Harry Rogers
Gruppe 2.1
2.1.1 John Quincy 2.1.2 Kent Peterson 2.1.3 Larry Newell
Gruppe 2.1.1
2.1.1.1 Max Milton 2.1.1.2 Norman Lamagna 2.1.1.3 Ollie Kensinton 2.1.4 John Frank
Gruppe 2.2
2.2.1 Mary Williams 2.2.2 Jane Frank 2.2.3 Jo Peterson 2.2.4 Angie Frost 2.2.5 Betty Bates 2.2.6 Connie Date 2.2.7 Donna Finster
Gruppe 2.3
2.3.1 Esther Gates 2.3.2 Fanny Hill 2.3.3 Geta Iota 2.3.4 Hillary Johnson 2.3.5 Ingrid Kent 2.3.6 Jill Larson 2.3.7 Kim Mulrooney 2.3.8 Lisa Nevins

Discussion

Almost any numbering scheme can be implemented with one or more xsl:number elements and suitable attributes. However, extensive use of xsl:number , especially with level="multiple", may slow a stylesheet. For deeply nested hierarchical numbers, pass the parent-level number down to children through a parameter to improve performance. Here is hierarchical numbering without using xsl:number at all:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="group">
    <xsl:param name="parent-level" select=" '' "/>
    <xsl:variable name="number" select="concat($parent-level,position( ))"/>
    <xsl:text>Group </xsl:text>
    <xsl:value-of select="$number"/>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates>
      <xsl:with-param name="parent-level" select="concat($number,'.')"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="person">
    <xsl:param name="parent-level" select=" '' "/>
    <xsl:variable name="number">
      <xsl:value-of select="concat($parent-level,position( ),' ')"/>
    </xsl:variable>
    <xsl:value-of select="$number"/>
    <xsl:value-of select="@name"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

This use of position is less convenient when the scheme requires letters for Roman numerals.

  

Back to top

 

 

 

back to top