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

如何在XQuery搜索中排除指定节点,为其余节点批量添加属性?

修改方案

你只需要调整两处XPath筛选逻辑,在原有判断「不存在TEST属性」的基础上,增加排除节点名的条件即可,用到XPath的local-name()函数获取当前节点名称,过滤掉值为a、b的节点。

完整修改后代码:

declare @xml XML = '
<root>
    <text><b>test2</b></text>
    <text>test</text>
    <a>测试a节点</a>
    <text>test</text>
    <text>test</text>
</root>
'
while @xml.exist('//*[not(@TEST) and not(local-name()=("a","b"))]') = 1
begin
    set @xml.modify('
        insert attribute TEST {"value"}
        into (//*[not(@TEST) and not(local-name()=("a","b"))])[1]
    ')
end
select @xml

修改说明

  • 原exist方法里的筛选条件//*[not(@TEST)],追加了and not(local-name()=("a","b")),排除名称为a、b的节点
  • modify方法里的插入目标节点筛选逻辑同步做了相同的调整,保证仅匹配符合要求的节点

内容的提问来源于stack exchange,提问作者Denis Kaminsky

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 13:24:03