2016-04-29 171 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