Hi,
It's a grouping problem. Probably the code below needs some optimization and
simplification... However, I tried to simplify the code as much as possible.
This approach with keys will be better(efficient) if the source XML will be bigger.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="weeks" match="Rank" use="Position/WeekID"/>
<xsl:key name="users" match="Rank" use="Position/UserID"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:for-each select="/Ranks/Rank/Position/WeekID[generate-id(ancestor::Rank) = generate-id(key('weeks', .)[1])]">
<xsl:sort select="." data-type="number"/>
<xsl:variable name="current-week-id" select="."/>
<tr>
<td>Week <xsl:value-of select="."/>:</td>
<td>
<xsl:for-each select="/Ranks/Rank/Position/UserID[generate-id(ancestor::Rank) = generate-id(key('users', .)[1])]">
<xsl:sort select="following-sibling::NickName" data-type="text"/>
<xsl:sort select="." data-type="number"/>
<!-- the following "xsl:if" element can be stripped out(keeping its content of course),
but in that case persons which are only, say, in weeks 1 through 2,
will be also listed for weeks greater than 2 with points 0 (or generally, for weeks in which they have no entry) -->
<xsl:if test="/Ranks/Rank/Position[WeekID = $current-week-id and UserID = current()]">
<xsl:value-of select="concat(following-sibling::NickName, '[', current(), ']')"/> has a total of <xsl:value-of select="sum(/Ranks/Rank/Position/Points[preceding-sibling::WeekID = $current-week-id and preceding-sibling::UserID = current()])"/> points
<br/>
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Regards,
Armen