Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xslt转换删除XmlNodes_Xml_Xslt - Fatal编程技术网

使用xslt转换删除XmlNodes

使用xslt转换删除XmlNodes,xml,xslt,Xml,Xslt,我需要从xml文档中删除一些xml节点。来源是 <root> <customElement> <child1></child1> <child2></child2> </customElement> <child3></child3> <child4></child4> </root>

我需要从xml文档中删除一些xml节点。来源是

<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
添加属性,以查看发生了什么:该属性最终位于输出中的
根节点中。)