XSLT根据XML节点排除项目

时间:2013-05-04 00:54:47

标签: xml xslt

我有一组数据,每个玩家都有一个<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>

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以<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>

答案 1 :(得分:0)

所以你想跳过任何ifb-player-stats的{​​{1}}吗?如果是这样,您只需修改ifb-goalie-stats中的路径:

即可
apply-templates