<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output doctype-system="http://www.w3.org/TR/html4/loose.dtd" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>

<xsl:variable name="ecf_url" select="'http://grading.bcfservices.org.uk/getref.php?ref='"/>

<xsl:key name="players" match="/chess/players/player" use="@ref"/>
<xsl:key name="teamByLeague" match="/chess/league/match/*" use="concat(../.., ',', team)"/>

<xsl:template name="string-replace-all">
  <xsl:param name="text"/>
  <xsl:param name="replace"/>
  <xsl:param name="by"/>
  <xsl:choose>
    <xsl:when test="contains($text,$replace)">
      <xsl:value-of select="substring-before($text,$replace)"/>
      <xsl:value-of select="$by"/>
      <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="substring-after($text,$replace)"/>
        <xsl:with-param name="replace" select="$replace"/>
        <xsl:with-param name="by" select="$by"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="string-format">
  <xsl:param name="text"/>
  <xsl:choose>
    <xsl:when test="contains($text,'0.5')">
      <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="$text"/>
      <xsl:with-param name="replace" select="'0.5'"/>
      <xsl:with-param name="by" select="'½'"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="$text"/>
      <xsl:with-param name="replace" select="'.5'"/>
      <xsl:with-param name="by" select="'½'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="/">
  <html>
  <head>
  <title>2009-10 League Results</title>
  <link rel="stylesheet" href="http://www.trowbridgechessclub.co.uk/styles/league_tables.css" type="text/css" media="print, projection, screen"/>
  <script type="text/javascript" src="http://www.trowbridgechessclub.co.uk/js/jquery-1.3.2.js"/>
  <script type="text/javascript" src="http://www.trowbridgechessclub.co.uk/js/jquery.tablesorter.min.js"/>
  <script type="text/javascript" src="http://www.trowbridgechessclub.co.uk/js/league_tables.js"/> 
  </head>
  <body>

  <div class="banner">
  <em>Menu</em><p/>
  <div class="slide_all"><xsl:element name="a"><xsl:attribute name="href">#</xsl:attribute>Match Results</xsl:element></div>
  <div><xsl:element name="a"><xsl:attribute name="href">#players</xsl:attribute>Player Records</xsl:element></div>
  </div>

  <xsl:apply-templates/>

  </body>
  </html>
</xsl:template>

<xsl:template match="chess/league">
  <div style="clear:left;">
  <div class="slide" title="Show or hide match results"><h2><xsl:value-of select="@name"/><span style="font-size:40%; margin-left:2em;">(Hide Results)</span></h2></div>

  <!-- League Table -->

  <div>
  <table class="tablesorter">
  <thead>
  <tr>
  <th width="100px">Team</th>
  <th width="55px">Played</th>
  <th width="55px">Won</th>
  <th width="55px">Drawn</th>
  <th width="55px">Lost</th>
  <th width="55px">For</th>
  <th width="55px">Against</th>
  <th width="55px">Points</th>
  </tr>
  </thead>

  <!-- Show teams, removing duplicates -->

  <tbody>
  <xsl:for-each select="match/home[         count(           . | key('teamByLeague', concat(../.., ',', team))[1]         ) = 1       ] | match/away[         count(           . | key('teamByLeague', concat(../.., ',', team))[1]         ) = 1       ]     ">
  <tr style="text-align: right;">
  <!-- Team -->
  <td style="font-size: 9pt; font-weight:bold"><xsl:value-of select="team"/></td>
  <!-- Played -->
  <td><xsl:value-of select="count(../../match/*[team=current()/team][not(score='')])"/></td>
  <!-- Won -->
  <td><xsl:value-of select="count(../../match/home[team=current()/team][score &gt; ../away/score] | ../../match/away[team=current()/team][score &gt; ../home/score])"/></td>
  <!-- Drawn -->
  <td><xsl:value-of select="count(../../match/home[team=current()/team][not(score='')][score = ../away/score] | ../../match/away[team=current()/team][not(score='')][score = ../home/score])"/></td>
  <!-- Lost -->
  <td><xsl:value-of select="count(../../match/home[team=current()/team][../away/score &gt; score] | ../../match/away[team=current()/team][../home/score &gt; score])"/></td>
  <!-- For -->
  <td><xsl:value-of select="sum(../../match/home[team=current()/team]/score[not(.='')] | ../../match/away[team=current()/team]/score[not(.='')])"/></td>
  <!-- Against -->
  <td><xsl:value-of select="sum(../../match/home[team=current()/team]/../away/score[not(.='')] | ../../match/away[team=current()/team]/../home/score[not(.='')])"/></td>
  <!-- Points -->
  <td style="font-weight:bold"><xsl:value-of select="2 * count(../../match/home[team=current()/team][score &gt; ../away/score] | ../../match/away[team=current()/team][score &gt; ../home/score]) + count(../../match/home[team=current()/team][not(score='')][score = ../away/score] | ../../match/away[team=current()/team][not(score='')][score = ../home/score])"/></td>
  </tr>
  </xsl:for-each>
  </tbody>

  </table>
  </div>
  <p/>

  <!-- Individual Match Results -->

  <xsl:for-each select="match[home/score!='']">
    <div class="match">
    <table>
    <tr>
      <th><xsl:value-of select="home/team"/></th>
      <th><xsl:element name="a">
      <xsl:choose>
      <xsl:when test="report!=''">
          <xsl:attribute name="href"><xsl:value-of select="report"/></xsl:attribute>
          <xsl:attribute name="title">Match Report <xsl:value-of select="date/@day"/>-<xsl:value-of select="date/@month"/>-<xsl:value-of select="date/@year"/></xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
          <xsl:attribute name="title"><xsl:value-of select="date/@day"/>-<xsl:value-of select="date/@month"/>-<xsl:value-of select="date/@year"/></xsl:attribute>
      </xsl:otherwise>
      </xsl:choose>
      <xsl:call-template name="string-format"><xsl:with-param name="text" select="home/score"/></xsl:call-template>-<xsl:call-template name="string-format"><xsl:with-param name="text" select="away/score"/></xsl:call-template>
      </xsl:element></th>
      <th><xsl:value-of select="away/team"/></th>
    </tr>
    <xsl:for-each select="gameinfo">
    <tr>

    <!-- Player table lookups -->

    <xsl:variable name="white" select="key('players',opponents/white/player-ref/@ref)"/> 
    <xsl:variable name="black" select="key('players',opponents/black/player-ref/@ref)"/> 

    <xsl:choose>
    <xsl:when test="(position() mod 2 = 0)">

      <!-- Home player has White on even boards -->
  
      <td style="text-align: right;">
        <xsl:choose>
        <xsl:when test="$white//text()">
          <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$ecf_url"/><xsl:value-of select="opponents/white/player-ref/@ref"/></xsl:attribute><xsl:value-of select="$white"/></xsl:element>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="opponents/white/player-ref/@ref"/>
        </xsl:otherwise>
        </xsl:choose>
      </td>
      <td style="text-align: center;"><xsl:call-template name="string-format"><xsl:with-param name="text" select="result/@res"/></xsl:call-template></td>
      <td class="b_player">
        <xsl:choose>
        <xsl:when test="$black//text()">
          <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$ecf_url"/><xsl:value-of select="opponents/black/player-ref/@ref"/></xsl:attribute><xsl:value-of select="$black"/></xsl:element>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="opponents/black/player-ref/@ref"/>
        </xsl:otherwise>
        </xsl:choose>
      </td>

    </xsl:when>
    <xsl:otherwise>

      <!-- Away player has White on odd boards -->
  
      <td class="b_player" style="text-align: right;">
        <xsl:choose>
        <xsl:when test="$black//text()">
          <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$ecf_url"/><xsl:value-of select="opponents/black/player-ref/@ref"/></xsl:attribute><xsl:value-of select="$black"/></xsl:element>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="opponents/black/player-ref/@ref"/>
        </xsl:otherwise>
        </xsl:choose>
      </td>

      <td style="text-align: center;">
<xsl:call-template name="string-format"><xsl:with-param name="text" select="concat(substring-after(result/@res, '-'),'-',substring-before(result/@res, '-'))"/></xsl:call-template>
      </td>
 
      <td>
        <xsl:choose>
        <xsl:when test="$white//text()">
          <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$ecf_url"/><xsl:value-of select="opponents/white/player-ref/@ref"/></xsl:attribute><xsl:value-of select="$white"/></xsl:element>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="opponents/white/player-ref/@ref"/>
        </xsl:otherwise>
        </xsl:choose>
      </td>

    </xsl:otherwise>
    </xsl:choose>
    </tr>
    </xsl:for-each>

    </table>
    <p/>
    </div>
  </xsl:for-each>
  </div>
</xsl:template>

<xsl:template match="chess/players">
  <div style="clear:left;">
  <h2>Player Records</h2>
  <table id="players">
  <thead>
  <tr>
  <th>Name</th>
  <th>Club</th>
  <th width="55px">Grade</th>
  <th width="55px">Played</th>
  <th width="55px">Won</th>
  <th width="55px">Drawn</th>
  <th width="55px">Lost</th>
  </tr>
  </thead>

  <tbody>
  <xsl:for-each select="player">
  <tr>
    <td><xsl:value-of select="."/></td>
    <td><xsl:value-of select="@team"/></td>
    <td><xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$ecf_url"/><xsl:value-of select="@ref"/></xsl:attribute><xsl:value-of select="@grade"/></xsl:element></td>
    <td><xsl:value-of select="count(//player-ref[@ref=current()/@ref])"/></td>
    <td><xsl:value-of select="count(//gameinfo[opponents/white/player-ref/@ref=current()/@ref and result/@res='1-0']) + count(//gameinfo[opponents/black/player-ref/@ref=current()/@ref and result/@res='0-1'])"/></td>
    <td><xsl:value-of select="count(//gameinfo[opponents/*/player-ref/@ref=current()/@ref and result/@res='0.5-0.5'])"/></td>
    <td><xsl:value-of select="count(//gameinfo[opponents/white/player-ref/@ref=current()/@ref and result/@res='0-1']) + count(//gameinfo[opponents/black/player-ref/@ref=current()/@ref and result/@res='1-0'])"/></td>
  </tr>
  </xsl:for-each>
  </tbody>

  </table>
  </div> 
</xsl:template>

</xsl:stylesheet>

