xml - XSLT XML 到 XML 如果特定子节点不存在则删除节点

标签 xml xslt

我是 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/

相关文章:

java - 使用 XSLT 将 HTML 表格转换为 CSV

java - 我无法从我的 Firebase 数据库中恢复数据

xml - XSL 根据子项向父项添加属性

XSLT 理解变量中的重复匹配

c# - 将 XmlDocument 内的所有文本转换为小写

javascript - 如何将 xsl 变量值传递给 javascript 函数

javascript - 默认使用 xsl :when test? 返回所有元素

php - 有条件地在 magento 布局中添加 block

Python 和 XML 处理

xml - XSL :FO float for displaying margin notes - breaks document