XSLT基于XML节点排除项



我有一组数据,其中每个玩家都有一个<ifb-regular-stats>节点。有些玩家有额外的<ifb-goalie-stats>节点。我要做的是排除所有在数据中有这个额外节点的玩家。(在我的例子中没有显示,但我有一个额外的表,将显示守门员的统计数据。)

示例XML数据(出于长度考虑,我在此示例中删除了一些子节点)

<sports-statistics>
  <sports-player-stats>
    <date year="2013" month="4" date="30" day="2"/>
    <soccer-ifb-player-stats>
      <ifb-player-stats>
      <player-name last-name="Test" first-name="Guy" shirt-name="" full-first="Guy" full-last="Test"/>
      <player-code global-id="537247" id="135523"/>
      <ifb-regular-stats type="team">
        <team-info global-id="26580" id="3340" location="Tulsa" name="TBears" alias="TUL" display-name="Tulsa"/>
        <games-played games="3"/>
        <games-started games="3"/>
        <goals goals="0" game-winning="0" own-goal="0" headers="0"/>
      </ifb-regular-stats>
      </ifb-player-stats>
      <ifb-player-stats>
      <player-name last-name="Abele" first-name="Kurt" shirt-name="" full-first="Kurt" full-last="Abele"/>
      <player-code global-id="537263" id="135524"/>
      <ifb-regular-stats type="team">
        <team-info global-id="26580" id="3340" location="Tulsa" name="TBears" alias="TUL" display-name="Tulsa"//>
        <games-played games="3"/>
        <games-started games="3"/>
        <goals goals="0" game-winning="0" own-goal="0" headers="0"/>
      </ifb-regular-stats>
      <ifb-goalie-stats type="team">
        <team-info global-id="26580" id="3340" location="Tulsa" name="TBears" alias="TUL" display-name="Tulsa"//>
        <games-played games="3"/>
        <games-started games="3"/>
        <record wins="0" losses="1" ties="2" percentage=".333"/>
      </ifb-goalie-stats>
  </sports-player-stats>
 </sports-statistics>
下面是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes"  encoding = "utf-8"   standalone = "yes"/>
  <xsl:variable name="fcPlayers">
    <map team="team" />
  </xsl:variable>
  <xsl:variable name="fcPlayersMap" select="msxsl:node-set($fcPlayers)/*" />
  <xsl:template match="/">
    <html>
      <head >
         <meta charset="utf-8" />
        <title> Player Stats</title>
      </head>
      <body>
        <table id="fcPlayers" >
          <tr >
            <th>Last</th>
            <th>First</th>
            <th  align="center">GP</th>
            <th  align="center">GS</th>
            <th  align="center">MP </th>
            <th  align="center">G</th>
            <th  align="center">GWG</th>
            <th  align="center">S</th>
            <th  align="center">SOG</th>
            <Th  align="center" >F</Th>
          </tr>
          <xsl:apply-templates select="//ifb-player-stats/ifb-regular-stats[@type = 'team']" >
            <xsl:sort select="../player-name/@last-name" />
          </xsl:apply-templates>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="ifb-player-stats/ifb-regular-stats[starts-with(team-info/@global-id, '26583')]">
    <xsl:variable name="ti" select="../player-name" />
    <tr>
      <td>
        <xsl:value-of select="$ti/@last-name" />
      </td>
      <td>
        <xsl:value-of select="$ti/@first-name" />
      </td>
      <td align="center">
        <xsl:value-of select="games-played/@games"/>
      </td>
      <td align="center">
        <xsl:value-of select="games-started/@games"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

提前感谢您的帮助!

所以你想跳过任何含有ifb-goalie-statsifb-player-stats ?如果是这样,您可以修改apply-templates:

中的路径。
<xsl:apply-templates 
   select="//ifb-player-stats[not(ifb-goalie-stats)]
               /ifb-regular-stats[@type = 'team']" >
  <xsl:sort select="../player-name/@last-name" />
</xsl:apply-templates>

您可以将<xsl:if>not()结合使用。

<xsl:template match="/">
  <xsl:if test="not(/sports-statistics/sports-player-stats/soccer-ifb-player-stats/ifb-goalie-stats)">
  // The ifb-goalie-stats node does not exist for this match, so render the template
  // Put everything else which is currently inside <xsl:template match="/"> here..
  </xsl:if>
</xsl:template>

最新更新