首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT -基于子元素的属性删除节点?

XSLT -基于子元素的属性删除节点?
EN

Stack Overflow用户
提问于 2015-09-12 04:30:14
回答 1查看 111关注 0票数 0

我试图根据特定子元素属性值的组合删除一个节点。在下面的示例输入中,如果vis-rank rankhome-rank rank都是'0‘,我希望删除整个’cfb-得分‘节点。我试过很多种表达方式,但似乎想不出解决办法。有时候,我可以让它完全删除所有的<cfb-scores>。有时候它什么都不做。

代码语言:javascript
复制
    <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是我的样式表已经在做的工作的输出,这涉及到大量的修剪、一些重命名和一些重构。下面是我完整的XSLT +原始输入的示例。

statxform.xslt:

代码语言:javascript
复制
<!--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样本:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-12 05:14:11

(编辑)

我想删除整个‘cfb-分数’节点,如果‘vis-军衔’和‘家庭-排名’都是'0‘。

呻吟,你为什么不制定有关输入的要求?这就是:

如果cfb-scorehome-team/team-rank中的@rank为0,则删除visiting-team/team-rank节点。

它很容易实现:

代码语言:javascript
复制
<xsl:template match="cfb-score[visiting-team/team-rank/@rank=0 and home-team/team-rank/@rank=0]"/>

顺便说一下,样式表的标识转换模板旁边有这样的注释:

代码语言:javascript
复制
<!--Copy all nodes into the workspace-->

这是一种误解。这里没有“工作区”。该模板建立了一个默认规则,其他具有较高优先级的模板可能会覆盖该规则。最后,每个节点只处理一次,由与其最匹配的模板处理。

我之所以提到这一点,是因为我认为造成最初混乱的是同样的误解:样式表只读取输入。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32535159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档