[xsl] Trouble using xsl:key under XSL 1.0

Subject: [xsl] Trouble using xsl:key under XSL 1.0
From: "Gareth Howells" <subscriptions@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 13 Dec 2007 02:56:01 -0000
Hello all,

I'm having a problem with xsl:key using XSL 1.0. It's almost 3am and I've been up for 3 days, so bear with me if I don't explain this well. Basically I have to extract data from an XML regarding a fantasy football league. There are 2 teams, with a collection of players, data regarding the number of points awarded for an event (such as a goal) and the number of each type of event achieved by each player, each week.

I've been able to get the total number of points for each player per week, and I've got the total number of points for a given team for every week. The problem I'm having is when trying to determine the number of points scored by a team for a given week. The XML file is here http://dmu.gforce-industries.co.uk/internet/fantasyLeague.xml and the stylesheet is here http://dmu.gforce-industries.co.uk/internet/transformFootball.xsl although below is what I believe to be the relevant code. As I said though, I am exhausted as I write this so bear with me.

As you can hopefully see, the code iterates over each results node, and during each iteration, it iterates over each team node, thus providing weekly results for each team.

The data in the XML means that for both weeks 1 and 2, team MU should have 20 points per week and team ARS should have 26 points per week, giving respective totals of 40 and 52. The results displayed for the first week are correct when I view the page (http://dmu.gforce-industries.co.uk/internet/processFootball.jsp - my server does not support JSP so you would have to copy the files to your own host to view the result), however the results for week 2 are not, the scores returned being the total points for every week (40 and 52) rather than for that week.

Can anyone suggest where I'm going wrong? Again, apologies if this email is not clear.


<xsl:for-each select="//results">

<xsl:variable name="week" select="@week" />

<xsl:key name="playerResultLookupByWeek" match="playerResult[../@week=$week]" use="@player" />

Week <xsl:value-of select="substring(@week,2,1)" />

<xsl:for-each select="//teams/team">

Team <xsl:value-of select="name" /> scored a total of <xsl:value-of select="
(sum(key('playerResultLookupByWeek',//teams/team[name=current()/name]/teamPlayers/teamPlayer)/g) * $gPoints)
(sum(key('playerResultLookupByWeek',//teams/team[name=current()/name]/teamPlayers/teamPlayer)/a) * $aPoints)
(sum(key('playerResultLookupByWeek',//teams/team[name=current()/name]/teamPlayers/teamPlayer)/gda) * $gdaPoints)
(sum(key('playerResultLookupByWeek',//teams/team[name=current()/name]/teamPlayers/teamPlayer)/gdcs) * $gdcsPoints)
(sum(key('playerResultLookupByWeek',//teams/team[name=current()/name]/teamPlayers/teamPlayer)/gdga) * $gdgaPoints)
"/> points this week



Current Thread