【问题标题】:XSLT - Remove full parent node if grandchild element contains specific textXSLT - 如果孙元素包含特定文本,则删除完整的父节点
【发布时间】:2015-11-02 11:36:40
【问题描述】:

我想使用 XSLT 1.0 进行 xml 到 xml 的转换。如果“Removeornot”值不是“1”,我需要从 XML 中删除完整节点。

我已经尝试过创建模板:

<xsl:template match="//Node[not(Grandchild0[not(Grand3child[not(Grand4child[not(Grand5child[not(Grand6child[not(Removeornot = 1)])])])])])]"/>

如果 xml 的级别较少,则此模板可以正常工作,但我无法将其应用到我的解决方案中。所以也许有人可以帮忙。

源 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Family>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>1</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>YES</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>NO</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
</Family>

这个 XSLT 只打印“Removeornot”值。完成此任务的其他方法是将此值用作变量,但据我所知,XSLT 中的变量是完全不可变的,我不能在 for-each 循环之外使用它们。如果值内容错误,我该如何使用此值删除节点?

XSLT 来源:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="v2">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
<xsl:template match="/">
      <xsl:for-each select="Family/Node">
      <xsl:call-template name="tests"></xsl:call-template>
      <!-- Is it possible to get variable value here to understand if node shloud be deleted?
             <xsl:if test="$tester = 1">
             <xsl:call-template name="copyit"></xsl:call-template>
              </xsl:if> -->
       </xsl:for-each>
 </xsl:template>

<xsl:template name="tests">
<!--      <xsl:for-each select="Family/Node"> -->
    <xsl:for-each select="Child2/Grandchild0">
        <xsl:for-each select="Grand3child/Grand4child">
            <xsl:for-each select="Grand5child/Grand6child">
                <xsl:value-of select="Removeornot"></xsl:value-of>
                    <!--  <xsl:variable name="tester" select="Removeornot" ></xsl:variable>-->
                 </xsl:for-each>
             </xsl:for-each>
          </xsl:for-each>
 <!--      </xsl:for-each> -->
</xsl:template>

<xsl:template name="copyit">
   <xsl:apply-templates select="@*|node()"/>
</xsl:template>

 <xsl:template match="@*|node()">
    <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
</xsl:template>

</xsl:stylesheet>

预期结果:

<?xml version="1.0" encoding="UTF-8"?>
<Family>
    <Node>
        <Child0>AAA</Child0>
        <Child1>BBB</Child1>
        <Child2>
            <Grandchild0>
                <Grand2child>DDD</Grand2child>
                <Grand3child>
                    <Grand4child>
                        <Grand5child>
                            <Grand6child>
                                <Removeornot>1</Removeornot>
                            </Grand6child>
                            <Grand6child1>QQQ </Grand6child1>
                        </Grand5child>
                        <Grand5child1>ZZZ </Grand5child1>
                    </Grand4child>
                </Grand3child>
                <Grand2child2>EEE</Grand2child2>
            </Grandchild0>
            <Grandchild2>FFF</Grandchild2>
            <Grandchild3>GGG</Grandchild3>
            <Grandchild4>HHH</Grandchild4>
            <Grandchild5>IIII</Grandchild5>
        </Child2>
    </Node>
</Family>

我也是 XSLT 新手,不胜感激。

【问题讨论】:

    标签: xml xslt-1.0


    【解决方案1】:

    如果“Removeornot”值不是“1”,我需要从 XML 中删除完整节点。

    那就是

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Node[not(.//Removeornot = 1)]" />
    </xsl:transform>
    
    1. 身份模板复制每个节点,除非有更具体的模板匹配。
    2. Node[not(.//Removeornot = 1)] 的另一个模板更具体 - 它不会输出任何与此处匹配的节点,这实际上会从输入中删除这些节点。

    【讨论】:

    • 谢谢!它适用于这个例子,但是如何匹配特定的 //Removeornot?例如:如果 Child1/../Removeornot 也有 sutch 元素?
    • 您的评论已经包含了这个问题的答案。给它五分钟,它会来找你的。
    • @Artūrs 那么,你知道了吗?
    • 好的,谢谢!有命名空间,这让我很困惑,但现在很清楚:) 关于命名空间的另一件事。源文件在不同的地方有一个“xmlns”,但转换后只剩下第一个“xmlns”值。有没有办法得到相同的副本?
    • @ArtūrsLelītis 很高兴听到! :) 至于您的问题:当您比较它们的序列化形式时,相同的 XML 文件不一定看起来很相似。当且仅当它们的 DOM 相同时,两个 XML 文档才是相同的。因此xmlns出现在序列化文件中的位置是完全无关紧要的,不同的序列化器只是有不同的输出。
    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多