<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

 <xsl:key name="teamByLeague" match="/chess/league/match/*" use="concat(../.., ',', team)"/>
 <xsl:template match="/">
 <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="chess/league">
 
 <xsl:variable name="teams" select="match/home[ count( . | key('teamByLeague', concat(../.., ',', team))[1] ) = 1 ] | match/away[ count( . | key('teamByLeague', concat(../.., ',', team))[1] ) = 1 ]"/>

 <div style="clear:left;">
 
 <h2>
 <xsl:value-of select="@name"/>
 </h2>

 <!-- League Table -->
 <div>
 <table>
 <thead>
 <tr>
 <th>Team</th>
 <xsl:for-each select="$teams">
 <th style="font-size: 9pt; font-weight:bold"><xsl:value-of select="team"/></th>
 </xsl:for-each>
 </tr>
 </thead>
 <!-- Show teams, removing duplicates -->
 <tbody>
 <xsl:for-each select="$teams">
 <xsl:variable name="hometeam" select="team"/>
 <tr style="text-align: right;">
 <!-- Home Team -->
 <td style="font-size: 9pt; font-weight:bold">
 <xsl:value-of select="$hometeam"/>
 </td>
  <xsl:for-each select="$teams">
  <xsl:choose>
  <xsl:when test="$hometeam = current()/team">
<td fgcolor="#ffffff" bgcolor="#ffffff">.</td>
 </xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="../../match/home[team=$hometeam][../away/team=current()/team]/score"/> - <xsl:value-of select="../../match/away[team=current()/team][../home/team=$hometeam]/score"/></td>
 </xsl:otherwise>
 </xsl:choose>
 </xsl:for-each>
 <!-- Home-->
 </tr>
 </xsl:for-each>
 </tbody>
 </table>
 </div>
 <p/>
 </div>
 </xsl:template>
 <xsl:template match="chess/players"/> 
</xsl:stylesheet>

