我想替换XML中的属性中的值。该属性在xml中多次出现。我怎样才能一次性更换这些
我的xml会像下面的内容一样:
<Example>
<A>
<B Type = "x">qqq</B>
<B Type = "x">www</B>
</A>
<C>
<D Type = "x">aaa</D>
<D Type = "x">uuu</D>
</C>
</Example>
我想用y替换所有x
发布于 2019-04-10 08:04:07
您无法使用(XML DML)的替换值一次替换所有内容。你必须循环完成。
declare @xml xml = '
<Example>
<A>
<B Type = "x">qqq</B>
<B Type = "x">www</B>
</A>
<C>
<D Type = "x">aaa</D>
<D Type = "x">uuu</D>
</C>
</Example>
'
while (select @xml.exist('//*[@Type = "x"]')) = 1
begin
set @xml.modify('replace value of (//*[@Type = "x"]/@Type)[1] with "y"')
end
select @xml
结果:
<Example>
<A>
<B Type="y">qqq</B>
<B Type="y">www</B>
</A>
<C>
<D Type="y">aaa</D>
<D Type="y">uuu</D>
</C>
</Example>
更新
用y替换值x和z:
while (select @xml.exist('//*[@Type = ("x","z")]')) = 1
begin
set @xml.modify('replace value of (//*[@Type = ("x","z")]/@Type)[1] with "y"')
end
用y替换所有值:
while (select @xml.exist('//*[@Type != "y"]')) = 1
begin
set @xml.modify('replace value of (//*[@Type != "y"]/@Type)[1] with "y"')
end
发布于 2019-04-10 09:21:10
您可以尝试以下方法。这里ns2zarejestrujStanZgodyAsync是主节点,我已经将idSystemy的值从13更新为38。
UPDATE @t
SET yourXML.modify('replace value of
(/ns2zarejestrujStanZgodyAsync/rejestrujStanZgody/idSystemy/text())[1] with ("38")')
您可以在此处查看演示 - XML节点更新
https://stackoverflow.com/questions/-100006558
复制相似问题