Using level="single" for Numbering
The <xsl:number>
examples in the previous topic use the level
attribute with a value of "multiple"
to assign a series of hierarchical number strings such as 1.1.01, 1.1.02, and so on.
The level
attribute can also take the values "single"
and "any"
. These are used to sequentially number elements, either at the same level in the document tree, or across all levels, respectively.
This topic demonstrates the effects of these two values by creating a non-empty template rule for the <tell>
elements in the sample XML document, cardgame.xml. This file is in Inserting Automatic Numbering Using position().
The following is the template rule with level="single"
.
<xsl:template match="tell">
<tr>
<td colspan="3" align="center" class="tell">
(Reaction #<xsl:number level="single"
count="tell"/>:
<xsl:value-of select="../@name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>)
</td>
</tr>
</xsl:template>
The value "single"
tells the XSLT processor to generate a sequential number that cycles through the elements whose names match the value of the count
attribute.
The formatted output below shows the results. Note the numbering assigned to the reactions of Jack and Jill as the cards are dealt.
The numbering generated from the <tell>
elements begins again with each player, so Jack's reactions are numbered 1 and 2, and Jill's reactions are numbered starting at 1 again.