XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / number

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The instruction xsl:number assigns the context node, or from XSLT 2.0 the node identified by the select attribute, a sequential number in document order reflecting its hierarchical position. Alternatively, it formats a number specified by its value attribute for output in the result document. It always creates a text node.

Classification

Instruction

Function group

Producing output in the result document

Introduced in

XSLT 1.0

Stylesheet position and permitted contents:

xsl:number occurs as an instruction inside sequence constructors. The element is always empty.

Attributes:

The usual standard attributes apply. In addition, the instruction xsl:number has optional attributes identifying the elements to count (count), their count value (value), the numbering hierarchy and counting depth (level, from), and the formatting of the numbering string for output (format, grouping-separator, grouping-size, lang, letter-value). Compared with XSLT 1.0, XSLT 2.0 adds the optional attributes select and ordinal.

It can also be used without any attributes, in which case the corresponding defaults apply.

count

Value

xpath-pattern

Usage

Optional

Introduced in

XSLT 1.0

The count attribute contains an XPath pattern identifying the element to count, either absolutely or relative to the current node or the node specified by the select attribute.

If no count attribute exists, numbering defaults to nodes of the same type as the current node or the node identified by select.

format

Value

{string} (also as an AVT)

Usage

Optional

Introduced in

XSLT 1.0

The format attribute describes the format of the output numbering string. Numbers, letters and separators may define numbering with digits, letters, Roman numerals or number words. Ordinal numbering is also possible (ordinal numbering).

Example format strings and their numbering sequences:

format="a)" produces lowercase letters followed by a closing parenthesis: a), b), c) ...

format="i." Roman numbering: i., ii., iii. ...

format="1.a" hierarchical numbering with numbers and lowercase letters: 1.a, 1.b, 2.a ... (see the table below for further sequences).

The format string format="1st" produces 1st, 2nd, 3rd, 4th, etc.; the format string format="first" produces first, second, third. Currently, only an English implementation is required. Alternatively, from XSLT 2.0, the ordinal attribute can be used.

The default is "1"; without a format attribute, xsl:number outputs simple numbers. The format string may be determined at runtime by an attribute value template.

from

Value

xpath-pattern

Usage

Optional

Introduced in

XSLT 1.0

The from attribute identifies the node serving as the starting point for counting. From the current node, the first matching node in reverse document order is selected. The starting node and its predecessors are excluded from counting.

By default, counting is unrestricted, equivalent to a pattern matching no node (for example: "*[false()]").

grouping-separator

Value

{char} (also as an AVT)

Usage

Optional

Introduced in

XSLT 1.0

The grouping-separator attribute must be used together with the grouping-size attribute and defines a separator character for splitting long numbers into digit groups. This is normally a period, or a comma in Anglo-American usage.

Note: Unless the group size is also specified by the grouping-size attribute, this attribute is ignored. An attribute value template can determine the character.

grouping-size

Value

{number} (also as an AVT)

Usage

Optional

Introduced in

XSLT 1.0

The grouping-size attribute determines group size when splitting long numbers. Its value must be an integer or an attribute value template yielding an integer. Groups of three are most common.

Note: This attribute is ignored unless a grouping-separator attribute is also present.

lang

Value

{nmtoken} (also as an AVT)

Usage

Optional

Introduced in

XSLT 1.0

The lang attribute specifies the language whose conventions govern the numbering string. Under RFC 1766, a two-letter country code according to ISO 639 represents the language.

Alternatively, an Internet Assigned Numbers Authority (IANA) code can be used with the prefix “i-” before the actual code. User-defined codes are also possible, marked by the prefix “x-”.

An attribute value template can generate the language code, for example for automatic stylesheet localization. The system environment’s language is the default.

letter-value

Value

{"alphabetic" | "traditional"} (also as an AVT)

Usage

Optional

Introduced in

XSLT 1.0

The letter-value attribute is needed when a numbering sequence’s starting symbol alone does not unambiguously determine its continuation. Permitted values are "alphabetic" and "traditional", which may also be supplied through an AVT. The default is "alphabetic".

Hebrew illustrates this attribute: both traditional and contemporary alphabets start with Aleph א but then use different letter sequences.

level

Value

"single" | "multiple" | "any"

Usage

Optional

Introduced in

XSLT 1.0

The level attribute determines counting depth within the document hierarchy. Its value is single, multiple or any. The default is "single".

All nodes matching the XPath pattern in the count attribute are counted; if no count attribute is given, nodes matching the current node’s name and type are counted. A from attribute, if present, determines the counting start point.

level="single"
The default value "single" counts all nodes on the preceding-sibling axis and also applies when the attribute is absent. The current node is counted too: with three countable predecessors, it receives the number four.

level="multiple"
This level lets xsl:number count several node types or elements on different levels, such as chapters, headings and sections, producing hierarchical numbering such as A.1.a.

Together with the count and format attributes, this permits almost arbitrary structures. First, a list of all ancestors is built; then preceding siblings are counted for all nodes matching the count pattern. The resulting hierarchical numbering follows the string in the format attribute.

level="any"
The attribute value level="any" tells the instruction to search backward in document order from the current node for all nodes matching the count pattern, regardless of their hierarchical position. Counting stops, if applicable, at the starting node identified by the from attribute. The current node counts if it matches the count pattern.

ordinal

Value

{string} (also as an AVT)

Usage

Optional

Introduced in

XSLT 2.0

The ordinal attribute requests ordinal numbering (1st, 2nd, 3rd, etc. or equivalent). Its form depends on the language and is further specified by the format attribute.

Note: The ordinal attribute does not exist in XSLT 1.0.

select

Value

xpath-expression

Usage

Optional

Introduced in

XSLT 2.0

The select attribute directly identifies the node to which the instruction applies. Selecting more than one node reports an error (ERR XTTE1000).

If there is no select attribute, xsl:number refers to the context node, so select="." can be considered the default. If the context item is not a node and there is also no value attribute, a type error is reported (ERR XTTE0990).

Note: The select attribute does not exist in XSLT 1.0.

value

Value

xpath-expression

Usage

Optional

Introduced in

XSLT 1.0

The value attribute contains an XPath expression evaluated to an integer and used as the numbering string. If a format attribute is also present, output is formatted accordingly. The value 0 is explicitly allowed for value.

Without a value attribute, xsl:number numbers the element according to its position in the tree, hence document order, taking any level and from attributes into account.

Note: If the value attribute is used, none of the level, count, select or from attributes may appear. Violating this is a static error (ERR XTSE0975). An error also occurs if the expression yields no integer xs:integer or evaluates to a value below 0 (ERR XTDE0980).

Purpose:

In the simplest case, the instruction xsl:number returns an integer identifying the context node’s position among same-named siblings at its level of the document hierarchy (example 1). The level attribute permits counting across hierarchy levels (examples 4 and 5), while the from attribute can set the counting start point.

Instead of deriving numbering from node position, the value attribute can force independent numbering. This matters especially when outputting sorted elements no longer in document order.

The XPath function fn:position() is used here (example 2). The format attribute of xsl:number can format its otherwise integer-only return value in any form. An alternative uses fn:position() together with format-number() and a xsl:decimal-format definition.

The format string:
The format attribute specifies numbering symbols and separators. The default uses Arabic digits with no separators.

Omitting the format attribute puts numbering immediately before the content; insert whitespace with xsl:text. Otherwise, spaces in the format string are significant characters.

The table below shows the effects of different format attribute values. Bold text in the “Effect” column is only for illustration:

format attribute value

Effect

(without a format attribute)

1List item

format=" "

1 List item

format="."

1.List item

format=". "

1. List item

format="001. "

001. List item

format="a "

a List item

format="A: "

A: List item

format="i) "

i) List item (lowercase Roman)

format="I "

I List item (uppercase Roman)

format="w"

one List item
(continues: two, three, four)

format="W"

ONE List item
(continues: TWO, THREE ...)

format="Ww"

One List item
(continues: Two, Three ...)

format="a#: "

a#: List item

Table: Effects of the format attribute.

Note: Actual output for “w”, “W” and “Ww” depends on the current language setting.

Numbering symbols, formally:
The format attribute generally specifies the first character determining the numbering sequence. The simplest is digit 1, the default, or a letter a, A, i or I.

Hebrew, Greek or Japanese Katakana characters can also serve as symbols. The character considered “one”, the starting point, is supplied as a Unicode Character entity.

Starting character

Numbering

a

Lowercase Western alphabet

A

Uppercase Western alphabet

i

Lowercase Roman numbering

I

Uppercase Roman numbering

א

Hebrew, alphabetic/traditional*

ა

Georgian, alphabetic/traditional*

α

Greek, alphabetic/traditional*

䀰

Old Slavonic, alphabetic/traditional*

ア

Japanese (Katakana)

イ

Japanese (Katakana, Iroha variant)

๑

Thai

(Note: * The letter-value attribute also applies to this sequence.)

Table: Starting characters for different numbering sequences.

Numbering with digits:
Simple cases use digits, giving numbering such as:

  • Without the format attribute: “1”, “2”, “3”, “4”, etc.

  • One or more leading zeros may precede digits in the format token. They appear in the output and are replaced with the corresponding digits as needed.

  • The format token "01" gives “01”, “02”, ... “09”, “10”, etc.

  • The format token "001" gives “001”, “002”, ... “099”, “100”, etc.

  • The ordinal attribute can also request ordinal numbering (ordinals). A nonempty attribute string initially suffices to add Anglo-American ordinal endings; the value supplied here, "yes", is essentially irrelevant to output:

  • format="1" ordinal="yes" gives “1st”, “2nd”, “3rd”, ...

  • Alternatively, another ordinal ending can be specified, marked by a preceding hyphen:

  • format="1" lang="it" ordinal="-º" gives “1º”, “2º”, “3º”, ...

  • Note: If an ordinal instruction cannot be performed, it is ignored and cardinal numbering is used instead.

Numbering with letters:
Instead of digits, numbering can use consecutive alphabetic letters. The initial letter of a particular alphabet generates its corresponding sequence.

  • The token "a" gives “a”, “b”, “c”, etc.

  • After “z”, counting continues with “aa”, “ab”, etc.; likewise in uppercase with the token "A".

  • Roman numbering gives “i”, “ii”, “iii”, “iv”, “v”, etc.

Numbering with number words:
Number-word sequences can use upper- or lowercase. The resulting sequences depend on the lang attribute’s value:

  • The token "w" gives “one”, “two”, “three”, “four”, ...

  • The token "W" gives “ONE”, “TWO”, “THREE”, “FOUR”, ...

  • The token "Ww" gives a capitalized sequence: “One”, “Two”, “Three”, ...

  • The lang attribute can select a number-word sequence in other languages; for example, with lang="de", format="w" gives “eins”, “zwei”, “drei”, ...

    The ordinal attribute can also be used here:

  • With format="w" lang="de" ordinal="-e", the sequence is “erste”, “zweite”, “dritte”, ...

  • With format="w" lang="de" ordinal="-er", the sequence is “erster”, “zweiter”, “dritter”, ...

  • With format="W" lang="it" ordinal="-o", the output is “Primero”, “Secundo”, “Terzo”, “Quarto”, ...

Additional characters in the format string:
Other characters may surround the numbering symbol in the output string. Besides or instead of a period or space, arbitrary punctuation can precede or follow the actual symbol.

Appropriate additions, possibly with hierarchical numbering, can produce output strings such as IV – (1), A [iii], 2a.:, 1.2.a), etc. The following characters can be used directly here:

. , ; : _ – ! ? $ % [ ] = ( ) # ' + * ~

As entities or character entities, further special characters are possible, such as:

  • & using &

  • < using &lt;

  • > using &gt;

  • § using &#167;

A curly brace may also be used, but must be escaped by doubling it: instead of { use {{. This is because the format attribute supports attribute value templates, where curly braces have a control function.

Examples:

Example 1 — Creating a numbered list:

Stylesheet:

<xsl:template match="liste">
  <p>
    <xsl:for-each select="listenelement">
      <xsl:number/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="."/><br/>
    </xsl:for-each>
  </p>
</xsl:template>

Output:

<p>1 Listenwert<br> 2 Listenwert<br> 3 Listenwert<br> ...</p>

The child elements <listenelement> of an element <liste> are numbered by xsl:number in source order, starting at 1 in each case. xsl:number produces only plain digits. Thus xsl:text must force a space between the numbering and the list item’s string value.

Example 2 — Creating a sorted, numbered list:

Stylesheet:

<xsl:template match="liste">
  <p>
    <xsl:for-each select="listenelement">
      <xsl:sort select="."/>
      <xsl:number value="position()" format="1. "/>
      <xsl:value-of select="."/><br/>
    </xsl:for-each>
  </p>
</xsl:template>

Output:

<p>1. Listenwert<br> 2. Listenwert<br> 3. Listenwert<br> ...</p>

Sorting list items by string value with xsl:sort requires the attribute value in xsl:number to output the sorted node list position rather than document position. Otherwise, nodes would be numbered in document order despite sorting, usually conflicting with the sorted order. The format attribute adds a bullet point and separating space. Letters or Roman numerals could equally be used.

Example 3 — Counting preceding elements with count:

Source document, excerpt:

<buch>
  <kapitel>
    <titel>Überschrift</titel>
    ...
  </kapitel>
  <kapitel>
    <titel>Überschrift</titel>
    ...
  </kapitel>
  <kapitel>
    <titel>Überschrift</titel>
    ...
  </kapitel>
  ...
</buch>

Stylesheet (excerpt):

<xsl:template match="kapitel/titel">
  <h1>
    <xsl:number count="kapitel"/>
    <xsl:apply-templates/>
  </h1>
</xsl:template>

Result document (excerpt):

...
<h1>1 Überschrift</h1>
... bla ...
<h1>2 Überschrift</h1>
... bla ...

Headings are to be numbered in the title template, but the parent <kapitel> element must be counted. The count attribute does this.

Example 4 — Counting throughout the document with level="any":

Stylesheet (excerpt):

<xsl:template match="bild">
  <!-- Bild darstellen, wie auch immer -->
  <xsl:text>Abb. </xsl:text>
  <xsl:number level="any"/>
  <xsl:text>:</xsl:text>
  <!-- Bildunterschrift erzeugen -->
</xsl:template>

Since the <bild> element is the current node, no count attribute is needed. Using level="any" counts all pictures that occurred earlier in the document than the current picture.

Result document (excerpt):

<p><img src="katze.gif">Abb. 57: die gemeine Hauskatze</p>

With level="single" or no level attribute, the processor stops at the first preceding node matching the count pattern and counts its preceding siblings. Results are correct only if target nodes reliably share a hierarchy level. Otherwise, as with figures, the entire document must be searched backward from the context node, without stopping at the level of the first count match. To prevent this stopping behavior, set the level attribute to "any".

Example 5 — Hierarchical numbering with level="multiple":

Source document (excerpt):

...
  <!-- die erste Liste -->
  <liste>
    <item>Erstes Element erste Liste</item>
    <item>Zweites Element erste Liste</item>
     ...
  </liste>
  <!-- die zweite Liste -->
  <liste>
    <item>Erstes Element zweite Liste</item>
    <item>Zweites Element zweite Liste</item>
    ...
  </liste>
  ...

Setting the level attribute to "multiple" allows more complex numbering. If the document has two or more lists of the form “<liste> contains <item>”, lists and their items can be counted together, assigning different symbols to different hierarchy levels with punctuation between them.

Stylesheet (excerpt):

<xsl:template match="liste/item">
  <div>
    <xsl:number level="multiple" count="liste | item" format="1. a) "/> 
    <xsl:apply-templates/>
  </div>
</xsl:template>

The level attribute alone is insufficient. Only together with the count and format attributes can almost arbitrary structures be achieved. During template processing the current node is the <item> element, from which the ancestor axis leads to the <liste> element. It and its preceding siblings—the lists preceding the surrounding list—are counted, prefixing the second list with “2”. At the same time, the preceding siblings of the <item> element within the list are counted, since it also matches the counting pattern of the xsl:number instruction.

Result document (excerpt):

...
<div>1. a) Erstes Element erste Liste</div>
<div>1. b) Zweites Element erste Liste</div>
...
<div>2. a) Erstes Element zweite Liste</div>
<div>2. b) Zweites Element zweite Liste</div>
...

The numbering string’s hierarchy in format follows document hierarchy from left to right. Its rightmost segment refers to the deepest element in the document tree, usually the current node. The count attribute merely names the elements to count; their listed order is irrelevant.

Element definition:

XSLT 1.0:

<!-- Category: instruction -->
<xsl:number 
     value = number-expression
     level = "single" | "multiple" | "any"
     count = pattern 
     from = pattern 
     format = { string }
     lang = { nmtoken }
     letter-value = { "alphabetic" | "traditional" }
     grouping-separator = { char }
     grouping-size = { number } 
/>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:number
     value? = expression
     select? = expression
     level? = "single" | "multiple" | "any"
     count? = pattern
     from? = pattern
     format? = { string }
     lang? = { nmtoken }
     letter-value? = { "alphabetic" | "traditional" }
     ordinal? = { string }
     grouping-separator? = { char }
     grouping-size? = { number } 
/>

   

  

back to top