RE: [xsl] Trouble using xsl:key under XSL 1.0

Subject: RE: [xsl] Trouble using xsl:key under XSL 1.0
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Thu, 13 Dec 2007 09:38:27 -0600
> <xsl:key name="playerResultLookupByWeek"
match="playerResult[../@week=$week]" use="@player" />
A big thing to watch out for here - the xsl:key match must be a pattern,
which is required to be context-free. (Same as the match pattern in
xsl:template.) And one of the big limitations to patterns is that you
cannot have variable references inside them. Even if the variable is
top-level, and even if it's something simple like
document('')/*/someElement, it can't be used in the match pattern.

Learn to start thinking around variables in your keys, and you'll end up
banging your head against the wall less often. :)

~ Scott


-----Original Message-----
From: Gareth Howells [mailto:subscriptions@xxxxxxxxxxxxxxxxxxxxxxx]
Sent: Wednesday, December 12, 2007 8:56 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Trouble using xsl:key under XSL 1.0

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.

Thanks,
Gareth

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

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

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

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

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

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

</xsl:for-each>

</xsl:for-each>

Current Thread