XSLT - 如果孙元素包含特定文本,则删除完整的父节点

XSLT - Remove full parent node if grandchild element contains specific text

我想使用 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 新手,非常感谢您的帮助。

I need to remove full node from XML, if "Removeornot" value is not "1".

那就是

<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)] 的另一个模板更具体 - 它不会为此处匹配的任何节点输出任何内容,这实际上从输入中删除了这些节点。