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

Inserting Automatic Numbering Using position()

This topic contains the initial sample files for this section, cardgame.xml and cardgame_orig.xsl. The XML file represents the playing cards dealt in a card game with two players. Each player receives five cards at first, and in the second hand of the game they can exchange up to three cards for new ones.

Two hands are dealt. During the first hand, Jack receives the Queen of Clubs, Ace of Spades, and so on, while Jill receives the 5 of Diamonds, 5 of Spades, and so on. In the second hand, Jack trades in three cards (look at the replace attributes to see which ones), and Jill only one.

The hand each player is dealt includes one or more <tell> elements. (A "tell" in card games is a player's reaction—often unconscious—to the cards they have received.)

Note the use of the position() function. This XPath function returns the number of the current node within whatever node-set is being processed. Because the node-set established by the template rule (match="hand") contains all <hand> elements in the document, the output will be numbered from 1 to 2, in document order.

Example

XML File (cardgame.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="cardgame_orig.xsl"?>
<cardgame>
    <hand>
        <player name="Jack">
            <card dealtID="card01">
                <rank>Q</rank><suit>Clubs</suit>
            </card>
            <card dealtID="card02">
                <rank>A</rank><suit>Spades</suit>
            </card>
            <card dealtID="card03">
                <rank>10</rank><suit>Hearts</suit>
            </card>
            <tell>twitches nervously</tell>
            <card dealtID="card04">
                <rank>8</rank><suit>Clubs</suit>
            </card>
            <card dealtID="card05">
                <rank>4</rank><suit>Spades</suit>
            </card>
            <tell>squints</tell>
        </player>
        <player name="Jill">
            <card dealtID="card06">
                <rank>5</rank><suit>Diamonds</suit>
            </card>
            <card dealtID="card07">
                <rank>5</rank><suit>Spades</suit>
            </card>
            <card dealtID="card08">
                <rank>6</rank><suit>Clubs</suit>
            </card>
            <card dealtID="card09">
                <rank>6</rank><suit>Spades</suit>
            </card>
            <card dealtID="card10">
                <rank>10</rank><suit>Diamonds</suit>
            </card>
            <tell>grins</tell>
        </player>
    </hand>
    <hand>
        <player name="Jack">
            <card dealtID="card11" replace="card03">
                <rank>4</rank><suit>Diamonds</suit>
            </card>
            <tell>mutters, "Uh-oh..."</tell>
            <card dealtID="card12" replace="card04">
                <rank>K</rank><suit>Spades</suit>
            </card>
            <card dealtID="card13" replace="card05">
                <rank>7</rank><suit>Spades</suit>
            </card>
            <tell>tugs at left ear</tell>
        </player>
        <player name="Jill">
            <card dealtID="card14" replace="card10">
                <rank>K</rank><suit>Diamonds</suit>
            </card>
            <tell>sips her drink</tell>
        </player>
    </hand>
</cardgame>

XSLT File (cardgame_orig.xsl)

The first version of the XSLT style sheet simply numbers the hands, using a template rule for the <hand> element:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
    xmlns="http://www.w3.org/TR/REC-html40"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Set up the HTML page and table styles. -->
<xsl:template match="/">
    <HTML>
        <HEAD>
            <TITLE>Card Game</TITLE>
            <STYLE type="text/css">
                h1 {background-color: gold;
                    font-family: Tahoma,Verdana,Arial,sans-serif;
                    font-size: 24pt}
                h2 {background-color: yellow;
                    font-family: Tahoma,Verdana,Arial,sans-serif;
                    font-size: 18pt}
                h3 {background-color: white;
                    font-family: Tahoma,Verdana,Arial,sans-serif;
                    font-size: 14pt;
                    font-weight: bold}
                th {background-color: silver;
                    font-family: Tahoma,Verdana,Arial,sans-serif}
                td {background-color: white;
                    font-family: Tahoma,Verdana,Arial,sans-serif}
                .page {width: 75%}
                .tell {font-weight: bold}
            </STYLE>
        </HEAD>
        <BODY>
            <DIV class="page">
                <xsl:apply-templates/>
            </DIV>
        </BODY>
    </HTML>
</xsl:template>

<!-- Suppress <tell> elements. -->
<xsl:template match="tell"/>

<xsl:template match="hand">
    <h1>Hand #<xsl:value-of select="position()"/></h1>
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

Formatted Output