我有以下XML
<Root xmlns:test="http://sample">
<Values>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="1">
</Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="1">
</Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2">
</Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2">
</Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="1">
</Value>
</Values>
</Root>
此处我尝试使用type=2
更新所有值元素到type = 3
。
有人可以告诉我这是怎么做到的吗?
答案 0 :(得分:2)
declare @XML xml =
'<Root xmlns:test="http://sample">
<Values>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="1"></Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="1"></Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2"></Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2"></Value>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="1"></Value>
</Values>
</Root>';
while @XML.exist('declare namespace d3p1="http://www.w3.org/2001/XMLSchema-instance";
/Root/Values/Value[@d3p1:type = "1"]') = 1
begin
set @XML.modify('declare namespace d3p1="http://www.w3.org/2001/XMLSchema-instance";
replace value of (/Root/Values/Value[@d3p1:type="1"]/@d3p1:type)[1]
with 2')
end
select @XML
结果:
<Root xmlns:test="http://sample">
<Values>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2" />
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2" />
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2" />
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2" />
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:type="2" />
</Values>
</Root>