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

如何使用XSLT根据指定GlobalID列表移除XML中匹配的父元素

XSLT批量移除匹配GlobalID的ConfigKeyValues节点方案

XSLT 1.0 兼容实现(匹配你原有代码的版本要求)

利用带特殊分隔符的字符串匹配实现多值判断,避免部分匹配错误:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <!-- 定义要排除的GlobalID列表,用|分隔,首尾也加|避免部分匹配 -->
 <xsl:variable name="exclude-ids" select="'|4EAED006DCDC|2AE33C272408|C3DE024CAC11|'" />

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <!-- 匹配所有GlobalId在排除列表中的ConfigKeyValues节点,直接移除 -->
 <xsl:template match="ConfigKeyValues[contains($exclude-ids, concat('|', GlobalId, '|'))]"/>

</xsl:stylesheet>

XSLT 2.0 简化实现(运行环境支持2.0及以上时使用)

直接使用序列类型存储待排除ID,语法更简洁:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <!-- 定义待排除的GlobalID序列 -->
 <xsl:variable name="exclude-ids" select="('4EAED006DCDC', '2AE33C272408', 'C3DE024CAC11')" />

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <!-- 批量匹配移除 -->
 <xsl:template match="ConfigKeyValues[GlobalId = $exclude-ids]"/>

</xsl:stylesheet>

处理后输出结果

你的示例XML运行上述代码后,输出如下:

<ConfigKeyValuesTable>
  <ConfigKeyValues>
    <KeyValue>false</KeyValue>
    <CreatedDate>2020-09-04T09:42:57.6685544-05:00</CreatedDate>
    <GlobalId>8E761269F5AA</GlobalId>
    <IsActive>1</IsActive>
    <ModifiedDate>2021-09-28T00:38:40.5845906-05:00</ModifiedDate>
  </ConfigKeyValues>
</ConfigKeyValuesTable>

内容的提问来源于stack exchange,提问作者ionian coder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 04:15:03