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

Example of Automatic Numbering with <xsl:number>

Example

This is the final example produced by the topics about automatic numbering with <xsl:number>.

XML File (cardgame.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="cardgame_xslnum.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_xslnum.xsl)

This is the final version of the XSLT file for the topics about automatic numbering with <xsl:number>.

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

<!-- Template rule for the document root: -->
<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>

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

<xsl:template match="player">
    <h2>
        Player 
        <xsl:number 
            level="multiple" 
            format="1.1" 
            count="hand | player"/>:
        <xsl:value-of select="@name"/>
    </h2>
    <table width="75%">
        <tr>
            <th>Hand.Player.Card</th>
            <th>Rank/Suit</th>
            <th>Replaces</th>
        </tr>
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="card">
    <tr>
        <td align="center" width="30%">
            <xsl:number
                level="multiple" 
                format="1.1.01" 
                count="hand | player | card"/>
        </td>
        <td width="40%">
            <xsl:value-of select="rank"/>
            <xsl:text> of </xsl:text>
            <xsl:value-of select="suit"/>
        </td>
        <td>
            <xsl:choose>
                <xsl:when test="@replace">
                    <xsl:value-of select="preceding::card[@dealtID=current()/@replace]/rank"/>
                    of <xsl:value-of select="preceding::card[@dealtID=current()/@replace]/suit"/>
                </xsl:when>
                <xsl:otherwise>--</xsl:otherwise>
            </xsl:choose>
        </td>
    </tr>
</xsl:template>

<xsl:template match="tell">
    <tr>
        <td colspan="3" align="center" class="tell">
            (Reaction #<xsl:number level="any" count="tell"/>:
            <xsl:value-of select="../@name"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="."/>)
        </td>
    </tr>
</xsl:template>

</xsl:stylesheet>

Formatted Output