[xsl] Grouping and calculating a score list

Subject: [xsl] Grouping and calculating a score list
From: Volker Witzel <v.witzel@xxxxxx>
Date: Fri, 10 Feb 2006 08:40:09 +0100
Dear all,

I'm trying to maintain a score list of a table tennis team in XML and
use XSLT to transform it to HTML. I'd also like to run it it with the
browser's XSL transformer, no addtl. server-side software can be used
and I'm currently limited to XSLT 1.0.

*XMLs*
_T3Crew.xsl_
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="T3Crew.xsl"?>
<T3Crew xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="T3Crew.xsd">
      <results date="2005-11-01">
          <match>
              <player id="OLG" score="11"/>
              <player id="MT" score="5"/>
          </match>
          <match>
              <player id="MT" score="11"/>
              <player id="JH" score="9"/>
          </match>
          ...
      </results>
      <results date="2005-10-18">
          <match>
              <player id="OLG" score="11"/>
              <player id="MT" score="7"/>
          </match>
          <match>
              <player id="OLG" score="11"/>
              <player id="JH" score="2"/>
          </match>
          <match>
              <player id="OLG" score="9"/>
              <player id="VW" score="11"/>
          </match>
          ...
      </results>
</T3Crew>

_T3Crew_Players.xml_
<?xml version="1.0" encoding="UTF-8"?>
<players>
      <player id="OLG" name="OLG Name" aka="Olli"/>
      <player id="MT" name="MT Name" aka="Mark"/>
      <player id="JH" name="JH Name" aka="Joe"/>
      <player id="VW" name="VW Name" aka="Volker"/>
</players>

*Stylesheet*
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
	<xsl:output method="html" indent="yes" encoding="ISO-8859-1"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
	<xsl:key name="players-by-id" match="match/player" use="@id"/>
	<xsl:strip-space elements="*"/>
	<xsl:variable name="playerDoc" select="document('T3Crew_Players.xml')"/>
	<xsl:template match="/">
		<html>
			<body>
				<h2>T3Crew - Hall of Fame<br/>(after <xsl:value-of
select="count(T3Crew/results)"/> Evevnings)</h2>
				<xsl:apply-templates select="T3Crew"/>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="T3Crew">
		<xsl:call-template name="displayPlayerSummary">
			<xsl:with-param name="tableID" select="'results_HOF'"/>
		</xsl:call-template>
		<xsl:apply-templates select="results"/>
	</xsl:template>

	<xsl:template match="results">
		<hr/>
		<h2>Result from  <xsl:value-of select="@date"/>	</h2>
		<xsl:call-template name="displayPlayerSummary">
			<xsl:with-param name="playerList" select="./match/player"/>
			<xsl:with-param name="tableID" select="concat('results_' , @date)"/>
		</xsl:call-template>
	</xsl:template>

	<xsl:template name="displayPlayerSummary">
		<xsl:param name="playerList" select="//match/player"/>
		<xsl:param name="tableID" select="'tableID'"/>
		<table class="sortable" id="{$tableID}">
		<tbody>
			<tr>
				<!--<th>Platz</th>-->
				<th>Name</th>
				<th># Matches</th>
				<th>Score</th>
			</tr>
			
			
			<xsl:for-each select="$playerList[count(. | key('players-by-id',
@id)[1]) = 1]">
				<xsl:sort select="@id"/>
				<xsl:variable name="id" select="@id"/>
			<tr>
				<!--<td>&#160;</td>-->
				<td><xsl:value-of select="$playerDoc/players/player[@id =
$id]/@aka"/></td>
				<td class="number"><xsl:value-of
select="count($playerList[@id=$id])"/></td>
				<td class="number"><xsl:value-of select="count($playerList[@id=$id
and @score &gt; 10])"/></td>
			</tr>
			</xsl:for-each>
		</tbody>
		</table>
	</xsl:template>
	</xsl:stylesheet>


---------8<-----



My problem is that the loop over the distinct players works only while processing the first result block and <xsl:for-each select="*$playerList[count(. | key('players-by-id', @id)[1]) = 1]*"> is simply an empty node set on the second iteration.

I really took my time to get a grip on this, but it seems that I reached
my XSL limit here. Any help greatly appreciated - otherwise I'd need to
go for a sports without scores ;-)

//Volker

Current Thread