Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
level = “any”
This option is useful when numbering objects within a document that have a numbering sequence of their own, independent of their position within the hierarchic structure. Examples are figures and illustrations, tables, equations, footnotes, and actions from a meeting.
The
count
attribute can usually be defaulted. For example, to number quotations within a document, you can write a template rule such as:
Again, the
count
attribute is useful when several different element types are included in the same numbering sequence, for example there might be a single sequence that includes both diagrams and photographs.
Note that each evaluation of
position()
function. If this isn't adequate, the alternative is to perform a second pass, to add the sequence numbers. You can do this by writing the result of the first pass to a variable. The following example extracts all the
glossary
is used to hold the temporary results. Note that if you want to use
for the numbering, this needs to be a tree rather than a flat sequence of parentless elements, because
always numbers nodes in terms of their position in a tree.
Imagine a source document that contains glossary definitions scattered throughout the document, in the form:
The relevant template looks like this:
In this example, however, the numbers could have been generated equally well on the first pass using the
position()
function.
The
from
attribute is useful for indicating where numbering should restart:
The above code would number footnotes consecutively within a chapter, starting again at 1 for each chapter.
Example: Numbering the Lines of a Poem
The following example numbers the lines of a poem, showing the number to the right of every fourth line. Assume the input structure contains a
Source
This stylesheet can be used with the source file
theHill.xml,
shown in Chapter 4 on page 167.
Stylesheet
This stylesheet is
poem.xsl
. It uses
mod
operator to get the remainder when the line number is divided by 4 (or some other number supplied as a parameter).
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
xmlns:xs=“http://www.w3.org/2001/XMLSchema”
xpath-default-namespace=“http://poetry.org/ns”>
Output
See
Figure 6-9
level = “multiple”
This option is typically used to produce the hierarchic sequence numbers often found in technical or legal documents; for example, 1.12.3, or A2(iii).
Note that an alternative way to produce such numbers is to use several calls on
level=“single”
and different
count
attributes, for example:
(
Another technique, which might be marginally faster, is to evaluate the chapter number once and pass it as a parameter to the template handling the section, and then pass both the chapter number and section number (or their concatenation) as parameters to the template handling each clause.
However, using
level=“multiple”
is convenient, and in some cases—particularly with recursive structures, where