Inserting Automatic Numbering Using <xsl:number>

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Inserting Automatic Numbering Using <xsl:number>

The <xsl:number> element is useful for generating hierarchical strings of numbers that indicate the depth to which a particular portion of a document is nested. For example, a hypothetical document outline might appear as follows:

I.    Major Heading #1

    I.A.    First sub-heading under Major Heading #1

            I.A.1.    ...

            I.A.2.    ...

    I.B.    Second sub-heading under Major Heading #1

II.    Major heading #2

...

The cardgame.xml file in Inserting Automatic Numbering Using position() is a hierarchy that lends itself to this kind of numbering. Each hand dealt is a level. Each hand level contains a level representing each player. For each player, there up to five cards dealt. The hands themselves have already been numbered, using the position() function. Now we'll use <xsl:number> to number the players and their cards.

The following is the general form of the start tag for an <xsl:number> element.

<xsl:number
    level="depthofnumbering"
    count="pattern"
    from="pattern"
    value="expression"
    format="formatspec"
    lang="langcode"
    letter-value="alphaortrad"
    grouping-separator="character"
    grouping-size="number">

All of the attributes are optional. The ones we'll be concerned with in this section (and the ones you will probably use most often) are level, count, and format.

Without any attributes at all, <xsl:number> behaves the same way as the position() function does, as demonstrated earlier.

Now we'll add the following template rule to our existing style sheet, cardgame_orig.xsl. This style sheet is available in Inserting Automatic Numbering Using position().

<xsl:template match="player">
    <h2>
        Player <xsl:number/>:
        <xsl:value-of select="@name"/>
    </h2>
    <xsl:apply-templates/>
</xsl:template>

The new template rule displays the each player's position within each hand, as in the level-2 headings shown here:

Suppose, however, that we want to number the players more carefully, using a combination of the hand number and the position of the <player> element within that hand. We can use <xsl:number> with level="multiple", level="single", or level="any".

This section continues with the following topics: