不同级别的 muenchian 分组节点

muenchian grouping nodes at different level

我有一个类似于以下文件的 xml 文件,我想使用 XSLT 1.0 将正确的团队和球员分组在一起。但不是在相关球队下显示正确的球员,而是在所有球队下显示所有球员。

我知道它在根元素中有不同的节点集,这可能是分组不起作用的原因,我什至不确定这是否可能,muenchian 分组的示例似乎都有一个节点设置在根元素内。

XML

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

    <football>
                    <!-- team details go here -->
      <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>
                      <!-- end of teams -->

                   <!-- player details go here -->
      <players>
        <player teamID="#ASNL">
          <playerFirstName>Hector</playerFirstName>
          <playerSurname>Bellerin</playerSurname>
          <position>RB</position>
          <goals>3</goals>
          <assists>5</assists>
        </player>

        <player teamID="#ASNL">
          <playerFirstName>Mesut</playerFirstName>
          <playerSurname>Ozil</playerSurname>
          <position>CAM</position>
          <goals>10</goals>
          <assists>20</assists>
        </player>

        <player teamID="#NUTD">
          <playerFirstName>Papiss</playerFirstName>
          <playerSurname>Cisse</playerSurname>
          <position>CF</position>
          <goals>15</goals>
          <assists>5</assists>
        </player>

        <player teamID="#NUTD">
          <playerFirstName>Tim</playerFirstName>
          <playerSurname>Krul</playerSurname>
          <position>GK</position>
          <goals>0</goals>
          <assists>3</assists>
        </player>
      </players>
                            <!-- end of players -->

    </football>

XSLT

<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:key name="teamKey" match="team" use="@teamCode"/>


  <xsl:template match="/">

    <xsl:for-each select="/football/teams/team[generate-id(.)=generate-id(key('teamKey', @teamCode)[1])]">
      <xsl:sort select="teamName"/>

      <teamDetails>
        <br />
        <b>Team Name: </b>
        <xsl:value-of select="teamName"/>
        <br />
        <b>Stadium: </b>
        <xsl:value-of select="stadium"/>
        <br />
        <b>Location: </b>
        <xsl:value-of select="location"/>
        <br />
      </teamDetails>

      <table>
        <br />
        <tr>
          <th>First Name</th>
          <th>Surname</th>
          <th>Position</th>
          <th>Goals</th>
          <th>Assists</th>
        </tr>

        <xsl:for-each select="/football/players/player[key('teamKey', @teamID)]">
          <tr>
            <td>
              <xsl:value-of select="playerFirstName"/>
            </td>
            <td>
              <xsl:value-of select="playerSurname"/>
            </td>
            <td>
              <xsl:value-of select="position"/>
            </td>
            <td>
              <xsl:value-of select="goals"/>
            </td>
            <td>
              <xsl:value-of select="assists"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:for-each>

  </xsl:template>
</xsl:stylesheet>

不需要任何muenchian分组她:

将团队的第一个循环更改为(并为当前团队设置一个变量):

<xsl:for-each select="/football/teams/team">
  <xsl:sort select="teamName"/>
  <xsl:variable name="thisTeam" select="." />

而第二个循环对于团队内的球员来说:

 <xsl:for-each 
        select="/football/players/player[ @teamID  = $thisTeam/@teamCode ]">

我建议你这样做:

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

<xsl:key name="player-by-team" match="player" use="@teamID"/>

<xsl:template match="/football">
    <xsl:for-each select="teams/team">
        <xsl:sort select="teamName"/>
        <b>Team Name: </b>
        <xsl:value-of select="teamName"/>
        <br />
        <b>Stadium: </b>
        <xsl:value-of select="stadium"/>
        <br />
        <b>Location: </b>
        <xsl:value-of select="location"/>
        <br />
        <table>
            <tr>
                <th>First Name</th>
                <th>Surname</th>
                <th>Position</th>
                <th>Goals</th>
                <th>Assists</th>
            </tr>
            <xsl:for-each select="key('player-by-team', @teamCode)">
                <tr>
                    <td>
                        <xsl:value-of select="playerFirstName"/>
                    </td>
                    <td>
                        <xsl:value-of select="playerSurname"/>
                    </td>
                    <td>
                        <xsl:value-of select="position"/>
                    </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>