counter iteration in XSLT

I would like to modify some existing code in XSLT to be able to iterate and display the counter.

Here is the code, where "Rank" has to be the counter, I cannot implement this elsewhere because the sorting is done here!

I don't know if my question is clear, but here is the bit of code:

<xsl:for-each select="Player">
      <xsl:sort select="Points" order="descending" data-type="number"/>
      <xsl:sort select="G" order="descending" data-type="number"/>
      <xsl:sort select="A" order="descending" data-type="number"/>
      <xsl:sort select="GP" order="ascending" data-type="number"/>
      <tr bgcolor="#E2EFED" border="0">
        <td align="center"><xsl:value-of select="Rank"/></td>
        <td align="center"><xsl:value-of select="Name"/></td>
        <td align="center"><xsl:value-of select="Team"/></td>   
      </tr>
</xsl:for-each>

Upvotes: 2

Views: 3362

Answers (2)

Carlos da Costa
Carlos da Costa

Reputation: 136

You can use the XPath position() function.

http://www.w3.org/TR/xpath/#function-position

In other words just change:

<td align="center"><xsl:value-of select="Rank"/></td>

to

<td align="center"><xsl:value-of select="position()"/></td>

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

Here is a complete example how to do this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
  <xsl:for-each select="*">
   <xsl:value-of select="concat('&#xA;', position(), '. ', name())"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>
 <a/>
 <b/>
 <c/>
 <d/>
 <e/>
</t>

the wanted result is produced:

1. a
2. b
3. c
4. d
5. e

Modify your code to this:

    <xsl:for-each select="Player">
        <xsl:sort select="Points" order="descending" data-type="number"/>
        <xsl:sort select="G" order="descending" data-type="number"/>
        <xsl:sort select="A" order="descending" data-type="number"/>
        <xsl:sort select="GP" order="ascending" data-type="number"/>
        <tr bgcolor="#E2EFED" border="0">
            <td align="center">
                <xsl:value-of select="position()"/>
            </td>
            <td align="center">
                <xsl:value-of select="Name"/>
            </td>
            <td align="center">
                <xsl:value-of select="Team"/>
            </td>
        </tr>
    </xsl:for-each>

Upvotes: 4

Related Questions