嵌套节点

时间:2016-04-29 15:19:10

标签: xml xslt

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

基本上我想为每个联赛(比赛)设置一个表格,以显示与每个球员相关的目标和助攻等统计数据。而我正在尝试使用密钥将这些链接在一起。

我一直在努力的是正确地将分数和联赛联系起来。目前它打印数据,但它显示相同的目标,并协助得分,无论正在打印哪个联赛。

  • 我试过像下面那样反转键 <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"/> 因为它似乎是在打印数据,除了它没有显示每个联盟的唯一值。即第一个联赛打印正确,但所有剩余的联赛表只是第一个联赛的副本。

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 个答案:

答案 0 :(得分: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