我是 XSL 的新手,找不到关于这个问题的信息。这仅适用于 XSLT 1.0,最终将从 XSLTproc 运行。
这是一个示例 XML
<root>
<node>
<data />
<child>
<grandchild />
</child>
<step-child action="removenode" />
</node>
<node>
<data />
<step-child action="removenode" />
</node>
</root>
基本上,我想保留所有内容,除了:
- 删除任何没有
<child>
的节点 - 删除所有
<step-child>
我只能弄清楚如何删除不需要的节点,但即使那样也是有问题的。非常感谢对此提供的任何帮助。
最佳答案
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
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>
<!--Remove node elements that do not have child elements,
and remove step-child elements -->
<xsl:template match="node[not(child)] | step-child"/>
</xsl:stylesheet>
关于xml - XSLT XML 到 XML 如果特定子节点不存在则删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10857626/