Search

I'm trying to think of a way to count totals in XSLT on a matched template.

I have a series of Rugby game stats that counts tries scored by each user..

I have the current match template working well:

<xsl:template match="matches/match-id/entry" mode="overview">
    <div class="article">
      <tr align="center">
        <td><a href="{$root}/{$url-language}/{$current-page}/{match-id/item/@id}"><xsl:value-of select="match-id"/></a></td>
        <td>Player Name here</td>
        <td><xsl:value-of select="tries"/></td>
        <td><xsl:value-of select="conversions"/></td>
        <td><xsl:value-of select="penalties"/></td>
        <td><xsl:value-of select="drop-goals"/></td>
        <td><xsl:value-of select="(drop-goals * 2) + (penalties * 3) + (conversions * 2) + (tries * 5)"/></td>
      </tr>
    </div>
  </xsl:template>

So for each entry.. i.e each player will output a total count of points..

How could I tally the count from the last TD of each entry, to output a total points count for the Match (match as in Game)?

So each match consists of a node match-id and has its points spread among several entries, right?

If that's the case I would say something along the lines of

<xsl:template match="match-id" mode="sum">
    <xsl:apply-templates select="entry[1]" mode="sum"/>
</xsl:template>

<xsl:template match="match-id/entry" mode="sum">
    <xsl:param name="sum" select="'0'"/>
    <xsl:apply-templates select="./following-sibling::entry[1]" mode="sum">
        <xsl:with-param name="sum" select="$sum + ./drop-goals"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="match-id/entry[last()]" mode="sum">
    <xsl:param name="sum" select="'0'"/>
    <xsl:value-of select="$sum + ./drop-goals"/>
</xsl:template>

these templates iterate over each entry one by one. If the matched node is not the last() one it will add its values and start the next iteration. If it is the last() one it will output the value.

you would call these templates using

<xsl:apply-templates select="match-id" mode="sum"/>

Ahh nice one Nils!

Yes each match is a node and several entries.. i.e players have points allocated to them.

Will have a play later. Looks very promising from first look.

Thanks again.

EDIT Hi Nils, does your XSLT take into account my total in the last TD? The value of each try in Rugby is 5 points and each conversion is 2. so my totals need to account for the value * actual points... i.e 2 tries = 2 x 5 (total points 10) and once each rows totals have been accounted, I am then looking for the total of each entries last TD to get Game points total, make sense?

To do that you just have to adjust lines 8 and 14 so they match the expression of your first example.

Ah right gotcha! thanks again.

Hi Nils,

I've got the totals counting for each row again, but am stuggling to see how I can get the total of each row into a count function:

Match id    Player Name Tries   Conversions Penalties   Drop Goals  Total Points
21  Andrew Minton   1   0   0   0   5
20  Andrew Minton   1   1   1   3   16
Total Points: 516

Notice my output just iterates over the entries and outputs the totals, but is there a way to count the totals for each row using a tweak to your example above? I thought that coming out of the entries and counting the match-id entry data in the "sum" mode would get me somewhere, but I'm unsure how to target a value that isn't in existence as such? any pointers?

It's cool, I used a bit of EXSL to count a nodeset variable. This page of info was useful! XML.com

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details