使用xslt转换删除XmlNodes
我需要从xml文档中删除一些xml节点。来源是使用xslt转换删除XmlNodes,xml,xslt,Xml,Xslt,我需要从xml文档中删除一些xml节点。来源是 <root> <customElement> <child1></child1> <child2></child2> </customElement> <child3></child3> <child4></child4> </root>
<root>
<customElement>
<child1></child1>
<child2></child2>
</customElement>
<child3></child3>
<child4></child4>
</root>
结果应该是
<root>
<child1></child1>
<child2></child2>
<child3></child3>
<child4></child4>
</root>
正如您所看到的,只有“customElement”元素被删除,但子元素仍然是结果文档的一部分。
如何使用xslt转换实现这一点。这里有一个简单的解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
<!-- identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- here we specify behavior for the node to be removed -->
<xsl:template match="customElement">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
您能告诉我们您的尝试吗?之前也有人问过同样的问题:+1。但是,@*
在后者应用模板中是无关的。(对于示例输入,请尝试删除
和
之间的空白,并向customElement
添加属性,以查看发生了什么:该属性最终位于输出中的根节点中。)