You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用XSLT移除XML中值为REMOVE的所有属性?

通用XSLT移除值为REMOVE的属性实现方案

可以通过XSLT的身份转换模板结合属性过滤实现这个需求,无需指定具体属性名称,完全通用。以下是可行的实现代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 身份转换模板:默认复制所有节点和属性 -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- 匹配值为REMOVE的属性,不做任何输出(即移除该属性) -->
    <xsl:template match="@*[. = 'REMOVE']"/>
</xsl:stylesheet>

代码说明

  • 身份转换模板是XSLT的基础模板,会原样复制XML中的所有节点(元素、文本、注释等)和属性,保证除目标属性外的其他内容完全保留。
  • 第二个模板专门匹配所有值等于REMOVE的属性,由于模板内部无任何输出逻辑,这类属性会被直接过滤掉,实现对任意属性的通用处理。

示例验证

输入XML:

<note>
    <to attribute1="REMOVE" attribute2="Keep it">Tove</to>
    <from>Jani</from>
    <heading hAttribute="REMOVE">Reminder</heading>
    <body bAttribute="REMOVE" bAttribute2="Any">Appointment</body>
</note>

转换后输出XML:

<note>
    <to attribute2="Keep it">Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body bAttribute2="Any">Appointment</body>
</note>

内容的提问来源于stack exchange,提问作者Enrico Barsali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 04:45:06