muenchian分组不同级别的节点

时间:2016-04-26 02:27:41

标签: xml xslt muenchian-grouping

我有一个类似于下面的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>

2 个答案:

答案 0 :(得分:1)

不需要任何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 ]">

答案 1 :(得分: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="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>