2013-05-04 103 views
0

我有一組數據,每個玩家都有一個<ifb-regular-stats>節點。一些玩家有一個額外的節點<ifb-goalie-stats>。我想要做的是排除在數據中具有該附加節點的任何播放器。 (在我的例子中未示出,但我有一個額外的表,這將只是顯示守門員統計資料。)XSLT根據XML節點排除項目

實施例的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> 

在此先感謝您的幫助!

回答

0

所以,你要跳過中有一個ifb-goalie-stats任何ifb-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> 
+0

完美!謝謝! – 2013-05-04 02:10:12

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>