首页 文章

删除子节点结果为空父节点

提问于
浏览
0

我是XSLT的新手,我按照下面的文章来实现我的要求 . 我的要求是如果满足子节点中的条件,则完全删除父节点 . 与下面的文章相同 .

deleting the parent node if child node is not present in xml using xslt

参考上文中的示例,我能够删除 <car> 下的内容 . 但是,我的源码xml有一个扭曲 . 扭曲是它有一个命名空间 .

例如:

源xml

<car xmlns="urn:test:v2xml">
 .....
</car>

输出xml:

<car/>

在我的实现中,结果是我的XML输出中的空车节点 . 我期待汽车节点被完全删除,但无论我尝试什么,空车节点仍保留在输出xml中 .

有没有办法完全删除该车节点?我怀疑我的命名空间与它有关,因为没有命名空间,它可以工作,标签被完全删除 .

2 回答

  • 0

    You'll need to specify the namespace for car, taking care of whether you want to squelch car or Car:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:v2="urn:test:v2xml"
                    version="1.0">
        <!--Identity template to copy all content by default-->
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="v2:car[not(Price)]"/>
    </xsl:stylesheet>
    
  • 0

    通过链接第二个转换过程来删除空节点,我能够实现我想要的 . 它被称为多次通过 . 因此,在单个XSLT样式表中,第一个输出是使用空节点创建的 . 然后在同一样式表中的第二个转换中,进行搜索以删除空节点 . 最终输出提供了清理结果 . 双程转换的示例如下:

    Two phase processing: Do not output empty tags from phase-1 XSLT 2.0 processing

相关问题