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>