Next / Previous / Contents / Shipman's homepage

9.5. xsl:number: Output an element number or formatted number

Sometimes you want to number things, like chapters in a book, or section numbers in an outline (1, 1.1, 1.2, and so on). That's what this element is for. It can also be used to format a number derived from an XPath expression.

The way elements are numbered depends on their position in a node-set. For example, if you have an element called volume-set whose children are volume elements, the template for the volume-set element will probably use this construct to format its children:

    <xsl:apply-templates select="volume"/>

The select="volume" XPath expression produces a node-set containing the volume children. So, the template that formats the volume element will get a node whose position in that set can be determined with the XPath function position(). Then you'll need an xsl:number construct to convert that position into a number in the output.

This feature has a lot of attributes that let you control the format of the number, and even its value:


If you just want to format the value of an XPath expression, use this attribute with the expression as its value. For example, xsl:number value="$bat-count"/ would output the value of the bat-count variable. If you want to number elements, though, don't use this attribute.


An XPath pattern that selects the nodes that are counted. For example, if you want your theorem and lemma tags to be counted on the same system, you can specify count="theorem|lemma".


This attribute controls whether we are numbering elements at just one level, or at multiple levels at once (such as an outline that has numbers like “”). Values are:

single Only elements at the same level as the current node are counted.
multiple Any ancestor nodes that match the count attribute are included to form a compount number. For example, if the current node is the 4th child of the 1st child of the 3rd child of its ancestor nodes that match the count value, the generated number will be 3.1.4.
any All the preceding and ancestor nodes that match the count expression are counted in a single sequence, and the number output is in that sequence.

The value of this attribute is a model for how the formatted number should look. Values include:

"1" Use Arabic numerals. You can supply leading zeroes if you like, so a value of "001" would fill each number with up to two leading zeroes.
"a" Use lowercase letters. The sequence is a, b, c, ..., z, aa, ab, ... and so on.
"A" Use capital letters.
"i" Use lowercase Roman numerals.
"I" Use uppercase Roman numerals.

You can also supply trailing punctuation, so for example format="1: " would follow each number with a colon and space.

There are some other attributes, not discussed here, that are useful for non-USA usages. Refer to the XSLT specification for more details.