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
been up for 3 days, so bear with me if I don't explain this well.
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
of points awarded for an event (such as a goal) and the number of each
of event achieved by each player, each week.

I've been able to get the total number of points for each player per
and I've got the total number of points for a given team for every week.
problem I'm having is when trying to determine the number of points
by a team for a given week. The XML file is here and the
stylesheet is here
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
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
correct when I view the page
( - my
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
that week.

Can anyone suggest where I'm going wrong? Again, apologies if this email
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
* $gPoints)
* $aPoints)
* $gdaPoints)
* $gdcsPoints)
* $gdgaPoints)
"/> points this week



Current Thread