XSLT XML to XML如果不存在特定子项,则删除节点

J. *_*ker 4 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>
Run Code Online (Sandbox Code Playgroud)

基本上,我想保留以下所有内容:

  • 删除没有的任何节点 <child>
  • 移除所有 <step-child>

我只能弄清楚如何删除不需要的节点,但即便如此也是有问题的.我真的很感激任何帮助.

Mad*_*sen 10

<?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>
Run Code Online (Sandbox Code Playgroud)

  • @torazaburo:如果是这样,那么请回答问题 - 而不是答案 - 尤其不是一个好的答案. (5认同)
  • @torazaburo:这不是作业,我是 Linux 系统管理员,对 XSLT 有点陌生,使用自动配置。顺便说一句,实际问题甚至比我问的还要多,这只是我自己遇到问题的部分。 (2认同)