如何使用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
相关产品推荐
相关产品推荐

