title: xslt keys for nested nodes
body: <p>I have an xml file which stores competitions, teams and players as shown below. I have stripped additional elements which are not relevant to the problem. </p>
  
  <p>Basically I want to set up a table for each league(competition) to display statistics like goals and assists relevant to each player. And I'm trying to use keys to link these together.</p>
  
  <p>What I've been struggling with is to correctly link the scores and the leagues. Currently it prints the data but it shows the same goals and assists score regardless of which league is being printed.</p>
  
  <ul>
  <li>I have tried reversing the key like follows
  <code>&lt;xsl:key name="scoreByLeague" match="score" use="@leagueID"/&gt;</code></li>
  <li>I have wrapped the <code>value-of select</code> statements for goals/assists in another <code>for-each</code></li>
  <li>I have tried changing the context of the <code>for-each</code> nodes</li>
  </ul>
  
  <p>My guess would be that the problem is at this line 
  <code>&lt;xsl:value-of select="scores/score[key('leagueScore', @leagueID)]/goals"/&gt;</code>
  as it seems to be printing the data, except it doesn't show the unique values for each league. i.e the first league is printed correctly but all the remaining league tables are just copies of the first.</p>
  
  <p>XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;?xml-stylesheet type="text/xsl" href="football.xslt"?&gt;
  
  &lt;football&gt;
    &lt;leagues&gt;
      &lt;league leagueCode="EPL"&gt;
        &lt;leagueName&gt;English Premier League&lt;/leagueName&gt;
      &lt;/league&gt;
      &lt;league leagueCode="FA"&gt;
        &lt;leagueName&gt;Football Association Cup&lt;/leagueName&gt;
      &lt;/league&gt;
    &lt;/leagues&gt;
  
    &lt;teams&gt;
      &lt;team teamCode="#ASNL"&gt;
        &lt;teamName&gt;Arsenal&lt;/teamName&gt;
        &lt;stadium&gt;Emirates Stadium&lt;/stadium&gt;
        &lt;location&gt;North London&lt;/location&gt;
      &lt;/team&gt;
  
      &lt;team teamCode="#NUTD"&gt;
        &lt;teamName&gt;Newcastle United&lt;/teamName&gt;
        &lt;stadium&gt;St James' Park&lt;/stadium&gt;
        &lt;location&gt;Newcastle Upon Tyne&lt;/location&gt;
      &lt;/team&gt;
    &lt;/teams&gt;
  
    &lt;players&gt;
      &lt;player teamID="#ASNL"&gt;
        &lt;playerFirstName&gt;Hector&lt;/playerFirstName&gt;
        &lt;playerSurname&gt;Bellerin&lt;/playerSurname&gt;
        &lt;position&gt;RB&lt;/position&gt;
        &lt;scores&gt;
          &lt;score leagueID="EPL" &gt;
            &lt;goals&gt;2&lt;/goals&gt;
            &lt;assists&gt;5&lt;/assists&gt;
          &lt;/score&gt;
          &lt;score leagueID="FA"&gt;
            &lt;goals&gt;1&lt;/goals&gt;
            &lt;assists&gt;3&lt;/assists&gt;
          &lt;/score&gt;
        &lt;/scores&gt;
      &lt;/player&gt;
  
      &lt;player teamID="#ASNL"&gt;
        &lt;playerFirstName&gt;Mesut&lt;/playerFirstName&gt;
        &lt;playerSurname&gt;Ozil&lt;/playerSurname&gt;
        &lt;position&gt;CAM&lt;/position&gt;
        &lt;scores&gt;
          &lt;score leagueID="EPL" &gt;
            &lt;goals&gt;8&lt;/goals&gt;
            &lt;assists&gt;15&lt;/assists&gt;
          &lt;/score&gt;
          &lt;score leagueID="FA"&gt;
            &lt;goals&gt;3&lt;/goals&gt;
            &lt;assists&gt;6&lt;/assists&gt;
          &lt;/score&gt;
        &lt;/scores&gt;
  
      &lt;/player&gt;
      &lt;player teamID="#NUTD"&gt;
        &lt;playerFirstName&gt;Papiss&lt;/playerFirstName&gt;
        &lt;playerSurname&gt;Cisse&lt;/playerSurname&gt;
        &lt;position&gt;CF&lt;/position&gt;
        &lt;scores&gt;
          &lt;score leagueID="EPL" &gt;
            &lt;goals&gt;15&lt;/goals&gt;
            &lt;assists&gt;5&lt;/assists&gt;
          &lt;/score&gt;
          &lt;score leagueID="FA"&gt;
            &lt;goals&gt;5&lt;/goals&gt;
            &lt;assists&gt;3&lt;/assists&gt;
          &lt;/score&gt;
        &lt;/scores&gt;
      &lt;/player&gt;
  
      &lt;player teamID="#NUTD"&gt;
        &lt;playerFirstName&gt;Tim&lt;/playerFirstName&gt;
        &lt;playerSurname&gt;Krul&lt;/playerSurname&gt;
        &lt;position&gt;GK&lt;/position&gt;
        &lt;scores&gt;
          &lt;score leagueID="EPL" &gt;
            &lt;goals&gt;0&lt;/goals&gt;
            &lt;assists&gt;5&lt;/assists&gt;
          &lt;/score&gt;
          &lt;score leagueID="FA"&gt;
            &lt;goals&gt;0&lt;/goals&gt;
            &lt;assists&gt;1&lt;/assists&gt;
          &lt;/score&gt;
        &lt;/scores&gt;
      &lt;/player&gt;
    &lt;/players&gt;
  
  &lt;/football&gt;
  </code></pre>
  
  <p>XSLT</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:output method="html" encoding="UTF-8"/&gt;
  
    &lt;xsl:key name="teamPlayer" match="team" use="@teamCode"/&gt;
    &lt;xsl:key name="leagueScore" match="league" use="@leagueCode"/&gt;
  
    &lt;xsl:template match="/football"&gt;
      &lt;xsl:for-each select="leagues/league"&gt;
  
        &lt;b&gt;Competition: &lt;/b&gt;
        &lt;xsl:value-of select="leagueName"/&gt;
        &lt;br /&gt;
  
        &lt;table&gt;
          &lt;tr&gt;
            &lt;th&gt;First Name&lt;/th&gt;
            &lt;th&gt;Surname&lt;/th&gt;
            &lt;th&gt;Team&lt;/th&gt;
            &lt;th&gt;Goals&lt;/th&gt;
            &lt;th&gt;Assists&lt;/th&gt;
          &lt;/tr&gt;
  
          &lt;xsl:for-each select="/football/players/player"&gt;
            &lt;tr&gt;
              &lt;td&gt;
                &lt;xsl:value-of select="playerFirstName"/&gt;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;xsl:value-of select="playerSurname"/&gt;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;xsl:value-of select="key('teamPlayer', @teamID)/teamName"/&gt;
              &lt;/td&gt;
  
              &lt;td&gt;
                &lt;xsl:value-of select="scores/score[key('leagueScore', @leagueID)]/goals"/&gt;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;xsl:value-of select="scores/score[key('leagueScore', @leagueID)]/assists"/&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
          &lt;/xsl:for-each&gt;
        &lt;/table&gt;
        &lt;br /&gt;
      &lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>