2016-04-29 129 views
0

我有一个存储比赛,球队和球员的xml文件,如下所示。我删除了与问题无关的其他元素。嵌套节点的xslt键

基本上我想建立一个表中的每个联盟(竞争),显示一个类似的目标数据和助攻相关的每个球员。我正在尝试使用键将这些链接在一起。

我一直在挣扎的正确链接得分和联赛。目前它会打印数据,但无论打印哪个联赛,它都会显示相同的目标和助攻分数。

  • 我试图扭转的关键类似如下 <xsl:key name="scoreByLeague" match="score" use="@leagueID"/>
  • 我已经包裹了进球/助攻value-of select声明中另一个for-each
  • 我曾试图改变for-each节点的环境

我的猜测是,这个问题是在这条线 <xsl:value-of select="scores/score[key('leagueScore', @leagueID)]/goals"/> ,因为它似乎是PR除了没有显示每个联盟的独特价值之外。即第一联赛的印刷正确,但其余的联赛表格只是第一联的副本。

XML

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="football.xslt"?> 

<football> 
    <leagues> 
    <league leagueCode="EPL"> 
     <leagueName>English Premier League</leagueName> 
    </league> 
    <league leagueCode="FA"> 
     <leagueName>Football Association Cup</leagueName> 
    </league> 
    </leagues> 

    <teams> 
    <team teamCode="#ASNL"> 
     <teamName>Arsenal</teamName> 
     <stadium>Emirates Stadium</stadium> 
     <location>North London</location> 
    </team> 

    <team teamCode="#NUTD"> 
     <teamName>Newcastle United</teamName> 
     <stadium>St James' Park</stadium> 
     <location>Newcastle Upon Tyne</location> 
    </team> 
    </teams> 

    <players> 
    <player teamID="#ASNL"> 
     <playerFirstName>Hector</playerFirstName> 
     <playerSurname>Bellerin</playerSurname> 
     <position>RB</position> 
     <scores> 
     <score leagueID="EPL" > 
      <goals>2</goals> 
      <assists>5</assists> 
     </score> 
     <score leagueID="FA"> 
      <goals>1</goals> 
      <assists>3</assists> 
     </score> 
     </scores> 
    </player> 

    <player teamID="#ASNL"> 
     <playerFirstName>Mesut</playerFirstName> 
     <playerSurname>Ozil</playerSurname> 
     <position>CAM</position> 
     <scores> 
     <score leagueID="EPL" > 
      <goals>8</goals> 
      <assists>15</assists> 
     </score> 
     <score leagueID="FA"> 
      <goals>3</goals> 
      <assists>6</assists> 
     </score> 
     </scores> 

    </player> 
    <player teamID="#NUTD"> 
     <playerFirstName>Papiss</playerFirstName> 
     <playerSurname>Cisse</playerSurname> 
     <position>CF</position> 
     <scores> 
     <score leagueID="EPL" > 
      <goals>15</goals> 
      <assists>5</assists> 
     </score> 
     <score leagueID="FA"> 
      <goals>5</goals> 
      <assists>3</assists> 
     </score> 
     </scores> 
    </player> 

    <player teamID="#NUTD"> 
     <playerFirstName>Tim</playerFirstName> 
     <playerSurname>Krul</playerSurname> 
     <position>GK</position> 
     <scores> 
     <score leagueID="EPL" > 
      <goals>0</goals> 
      <assists>5</assists> 
     </score> 
     <score leagueID="FA"> 
      <goals>0</goals> 
      <assists>1</assists> 
     </score> 
     </scores> 
    </player> 
    </players> 

</football> 

XSLT

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" encoding="UTF-8"/> 

    <xsl:key name="teamPlayer" match="team" use="@teamCode"/> 
    <xsl:key name="leagueScore" match="league" use="@leagueCode"/> 

    <xsl:template match="/football"> 
    <xsl:for-each select="leagues/league"> 

     <b>Competition: </b> 
     <xsl:value-of select="leagueName"/> 
     <br /> 

     <table> 
     <tr> 
      <th>First Name</th> 
      <th>Surname</th> 
      <th>Team</th> 
      <th>Goals</th> 
      <th>Assists</th> 
     </tr> 

     <xsl:for-each select="/football/players/player"> 
      <tr> 
      <td> 
       <xsl:value-of select="playerFirstName"/> 
      </td> 
      <td> 
       <xsl:value-of select="playerSurname"/> 
      </td> 
      <td> 
       <xsl:value-of select="key('teamPlayer', @teamID)/teamName"/> 
      </td> 

      <td> 
       <xsl:value-of select="scores/score[key('leagueScore', @leagueID)]/goals"/> 
      </td> 
      <td> 
       <xsl:value-of select="scores/score[key('leagueScore', @leagueID)]/assists"/> 
      </td> 
      </tr> 
     </xsl:for-each> 
     </table> 
     <br /> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

回答

1

如果我理解这个正确的,你想做的事:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" encoding="UTF-8"/> 

<xsl:key name="score-by-league" match="score" use="@leagueID"/> 
<xsl:key name="team" match="team" use="@teamCode"/> 

<xsl:template match="/football"> 
    <xsl:for-each select="leagues/league"> 
     <b>Competition: </b> 
     <xsl:value-of select="leagueName"/> 
     <br /> 
     <table> 
      <tr> 
       <th>First Name</th> 
       <th>Surname</th> 
       <th>Team</th> 
       <th>Goals</th> 
       <th>Assists</th> 
      </tr> 
      <xsl:for-each select="key('score-by-league', @leagueCode)"> 
      <tr> 
       <td> 
        <xsl:value-of select="ancestor::player/playerFirstName"/> 
       </td> 
       <td> 
        <xsl:value-of select="ancestor::player/playerSurname"/> 
       </td> 
       <td> 
        <xsl:value-of select="key('team', ancestor::player/@teamID)/teamName"/> 
       </td> 
       <td> 
        <xsl:value-of select="goals"/> 
       </td> 
       <td> 
        <xsl:value-of select="assists"/> 
       </td> 
      </tr> 
      </xsl:for-each> 
     </table> 
     <br /> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

得到以下结果(渲染):

enter image description here

+0

完美!谢谢迈克尔。 – nod64