XSLT - 根据子元素的属性删除节点?

时间:2015-09-12 04:30:14

标签: xml xslt

我正在尝试根据特定子元素属性值的组合删除节点。在下面的示例输入中,如果vis-rank rankhome-rank rank都为'0',我想删除整个'cfb-score'节点。我尝试了很多表达式,但似乎无法提出解决方案。有时我可以让它完全删除所有<cfb-scores>。有时它什么都不做。

    <cfb-score>
    <gamecode code="201509030004" global-id="1526672"/>
    <source id="1"/>
    <gametype id="1" type="Regular Season" detail="" round="" round-name=""/>
    <gamestate status-id="4" status="Final" quarter="4" minutes="" seconds="" team-possession-id="" yards-from-goal="" down="" distance="" segment-number="4" active-state="false"/>
    <outcome-visit outcome-id="2" outcome="Loss" conf-outcome-id="0" conference-outcome="No game"/>
    <outcome-home outcome-id="1" outcome="Win" conf-outcome-id="0" conference-outcome="No game"/>
    <vis-name name="Braves" alias="Alc"/>
    <vis-conf conference="Southwestern Athletic" abbrev="SWAC" id="23"/>
    <vis-rank rank="0" playoff-rank=""/>
    <vis-score score="6" timeouts-left="0"/>
    <home-name name="Yellow Jackets" alias="GaTech"/>
    <home-conf conference="Atlantic Coast" abbrev="ACC" id="1"/>
    <home-rank rank="16" playoff-rank=""/>
    <home-score score="69" timeouts-left="2"/>
    </cfb-score>
    <cfb-score>
    <gamecode code="201509030202" global-id="1528079"/>
    <source id="1"/>
    <gametype id="1" type="Regular Season" detail="" round="" round-name=""/>
    <gamestate status-id="4" status="Final" quarter="4" minutes="" seconds="" team-possession-id="" yards-from-goal="" down="" distance="" segment-number="4" active-state="false"/>
    <outcome-visit outcome-id="2" outcome="Loss" conf-outcome-id="0" conference-outcome="No game"/>
    <outcome-home outcome-id="1" outcome="Win" conf-outcome-id="0" conference-outcome="No game"/>
    <vis-name name="Wildcats" alias="Vill"/>
    <vis-conf conference="Colonial Athletic Association" abbrev="CAA" id="98"/>
    <vis-rank rank="0" playoff-rank=""/>
    <vis-score score="15" timeouts-left="0"/>
    <home-name name="Huskies" alias="UConn"/>
    <home-conf conference="American Athletic Conference" abbrev="AAC" id="122"/>
    <home-rank rank="0" playoff-rank=""/>
    <home-score score="20" timeouts-left="2"/>
    </cfb-score>

我想我应该指出上面的XML是我的样式表已经在做的OUTPUT,这涉及很多修剪,一些重命名和一些重组。我的完整XSLT +原始输入的样本如下。

statxform.xslt:

<!--STATS.COM >> PADS XML TRANSFORMER -BY JAKE BALLARD 2015-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Copy all nodes into the workspace-->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<!--Delete stuff we don't care about-->
<xsl:template match="team-city|team-code|quarter|record|date|time|local-game-date|local-time|week|coverage|bowl|stadium"/>
<!--We need to rename stuff in the visiting-team 
and home-team nodes with a unique prefix so they 
are still distinguishible after flattening-->
<!--Rename Visitor Children-->
<xsl:template match="visiting-team/team-name">
    <vis-name>
        <xsl:apply-templates select="@*|visiting-team/team-name"/>
    </vis-name>
</xsl:template>
<xsl:template match="visiting-team/team-conference">
    <vis-conf>
        <xsl:apply-templates select="@*|visiting-team/team-conference"/>
    </vis-conf>
</xsl:template>
<xsl:template match="visiting-team/team-rank">
    <vis-rank>
        <xsl:apply-templates select="@*|visiting-team/team-rank"/>
    </vis-rank>
</xsl:template>    
<xsl:template match="visiting-team/linescore">
    <vis-score>
        <xsl:apply-templates select="@*|visiting-team/linescore"/>
    </vis-score>
</xsl:template>
<!--Rename Home Children-->
<xsl:template match="home-team/team-name">
    <home-name>
        <xsl:apply-templates select="@*|home-team/team-name"/>
    </home-name>
</xsl:template>
<xsl:template match="home-team/team-conference">
    <home-conf>
        <xsl:apply-templates select="@*|home-team/team-conference"/>
    </home-conf>
</xsl:template>
<xsl:template match="home-team/team-rank">
    <home-rank>
        <xsl:apply-templates select="@*|home-team/team-rank"/>
    </home-rank>
</xsl:template>    
<xsl:template match="home-team/linescore">
    <home-score>
        <xsl:apply-templates select="@*|home-team/linescore"/>
    </home-score>
</xsl:template>
<!--Delete Parent Nodes for Visiting Team and Home Team, 
but keep contents under <cfb-score> so PADS has a unified table to peruse-->    
<xsl:template match="visiting-team" >
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="home-team" >
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="cfb-score/*[@rank = '0' ]"/>
<!--ROLL TIDE-->
</xsl:stylesheet>

在上一个模板中,您可以看到我最近尝试过滤未排名的游戏。它似乎什么都不做。

input.xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<sports-statistics>
<sports-scores>
<date year="2015" month="9" date="5" day="6"/>
<time hour="23" minute="32" second="09" timezone="Eastern" utc-hour="-4" utc-minute="00"/>
<version number="6"/>
<league global-id="16" name="College Football" alias="CFB" display-name=""/>
<season season="2015"/>
    <cfb-scores>
 <cfb-score>
   <date year="2015" month="9" date="3" day="4"/>
   <time hour="18" minute="00" timezone="Eastern" utc-hour="-4" utc-minute="00"/>
   <local-game-date year="2015" month="9" date="3" day="4"/>
   <local-time hour="18" minute="00"/>
   <week week="2"/>
   <gamecode code="201509030070" global-id="1526673"/>
   <coverage level="30"/>
   <source id="2"/>
   <gametype id="1" type="Regular Season" detail="" round="" round-name=""/>
   <stadium name="Bank of America Stadium" city="Charlotte" state="North Carolina" state-id="33" country-name="United States" country-id="1" id="1022" global-id="3098"/>
   <bowl id="0" name="" full-name=""/>
   <gamestate status-id="4" status="Final" quarter="4" minutes="" seconds="" team-possession-id="" yards-from-goal="" down="" distance="" segment-number="4" active-state="false"/>
   <outcome-visit outcome-id="2" outcome="Loss" conf-outcome-id="0" conference-outcome="No game"/>
   <outcome-home outcome-id="1" outcome="Win" conf-outcome-id="0" conference-outcome="No game"/>
   <visiting-team>
     <team-name name="Tar Heels" alias="UNC"/>
     <team-city city="North Carolina"/>
     <team-code id="6" global-id="3412" division="1"/>
     <team-conference conference="Atlantic Coast" abbrev="ACC" id="1"/>
     <team-rank rank="0" playoff-rank=""/>
     <record wins="0" losses="1" ties="0" pct=".000"/>
     <linescore score="13" timeouts-left="0">
        <quarter quarter="1" score="7"/>
        <quarter quarter="2" score="6"/>
        <quarter quarter="3" score="0"/>
        <quarter quarter="4" score="0"/>
     </linescore>
   </visiting-team>
   <home-team>
     <team-name name="Gamecocks" alias="SC"/>
     <team-city city="South Carolina"/>
     <team-code id="70" global-id="3475" division="1"/>
     <team-conference conference="Southeastern" abbrev="SEC" id="8"/>
     <team-rank rank="0" playoff-rank=""/>
     <record wins="1" losses="0" ties="0" pct="1.000"/>
     <linescore score="17" timeouts-left="2">
        <quarter quarter="1" score="0"/>
        <quarter quarter="2" score="10"/>
        <quarter quarter="3" score="0"/>
        <quarter quarter="4" score="7"/>
     </linescore>
   </home-team>
 </cfb-score>
 <cfb-score>
   <date year="2015" month="9" date="3" day="4"/>
   <time hour="18" minute="00" timezone="Eastern" utc-hour="-4" utc-minute="00"/>
   <local-game-date year="2015" month="9" date="3" day="4"/>
   <local-time hour="18" minute="00"/>
   <week week="2"/>
   <gamecode code="201509030210" global-id="1530288"/>
   <coverage level="30"/>
   <source id="1"/>
   <gametype id="1" type="Regular Season" detail="" round="" round-name=""/>
   <stadium name="Bright House Networks Stadium" city="Orlando" state="Florida" state-id="9" country-name="United States" country-id="1" id="2213" global-id="5790"/>
   <bowl id="0" name="" full-name=""/>
   <gamestate status-id="4" status="Final" quarter="4" minutes="" seconds="" team-possession-id="" yards-from-goal="" down="" distance="" segment-number="4" active-state="false"/>
   <outcome-visit outcome-id="1" outcome="Win" conf-outcome-id="0" conference-outcome="No game"/>
   <outcome-home outcome-id="2" outcome="Loss" conf-outcome-id="0" conference-outcome="No game"/>
   <visiting-team>
     <team-name name="Golden Panthers" alias="FIU"/>
     <team-city city="FIU"/>
     <team-code id="2307" global-id="5090" division="1"/>
     <team-conference conference="Conference USA" abbrev="CUSA" id="72"/>
     <team-rank rank="0" playoff-rank=""/>
     <record wins="1" losses="0" ties="0" pct="1.000"/>
     <linescore score="15" timeouts-left="1">
        <quarter quarter="1" score="3"/>
        <quarter quarter="2" score="0"/>
        <quarter quarter="3" score="6"/>
        <quarter quarter="4" score="6"/>
     </linescore>
   </visiting-team>
   <home-team>
     <team-name name="Knights" alias="UCF"/>
     <team-city city="UCF"/>
     <team-code id="210" global-id="3615" division="1"/>
     <team-conference conference="American Athletic Conference" abbrev="AAC" id="122"/>
     <team-rank rank="0" playoff-rank=""/>
     <record wins="0" losses="1" ties="0" pct=".000"/>
     <linescore score="14" timeouts-left="0">
        <quarter quarter="1" score="7"/>
        <quarter quarter="2" score="7"/>
        <quarter quarter="3" score="0"/>
        <quarter quarter="4" score="0"/>
     </linescore>
   </home-team>
 </cfb-score>
 </cfb-scores>
 </sports-scores>
 </sports-statistics>

1 个答案:

答案 0 :(得分:1)

<强> (编辑)的

  

如果'vis-rank rank',我想删除整个'cfb-score'节点   并且'排名等级'都是'0'。

[呻吟]为什么不制定关于输入的要求?那就是:

  

如果两者中cfb-score为0,则删除@rank节点   visiting-team/team-rankhome-team/team-rank

使用以下方法很容易实现:

<xsl:template match="cfb-score[visiting-team/team-rank/@rank=0 and home-team/team-rank/@rank=0]"/>

顺便说一句,你的样式表在身份转换模板旁边有这个评论:

<!--Copy all nodes into the workspace-->

这是一种误解。这里没有“工作空间”。模板建立默认规则,其他具有较高优先级的模板可以覆盖。最终,每个节点仅由最匹配的模板处理一次。

我之所以提到这一点,是因为我认为这种误解导致了最初的混淆:样式表只读 输入。