I have an XML like below:
<BODY>
<RECORD>
<PA0002_NATIO>CH</PA0002_NATIO>
<PA0001_CITY>Lugano</PA0001_CITY>
<PA0005_VALUE>1000</PA0005_VALUE>
</RECORD>
<RECORD>
<PA0002_NATIO>DE</PA0002_NATIO>
<PA0001_CITY>Berlin</PA0001_CITY>
<PA0005_VALUE>2000</PA0005_VALUE>
</RECORD>
<RECORD>
<PA0002_NATIO>IT</PA0002_NATIO>
<PA0001_CITY>Roma</PA0001_CITY>
<PA0005_VALUE>3000</PA0005_VALUE>
</RECORD>
</BODY>
I would like to change the value for the tag <PA0002_NATIO> within all <RECORD> nodes and in order to do that I count the number of the <RECORD> nodes and I do a loop like this, the new value is taken from a table.
if @countNodes > 0
begin
set @indexCount = 1
while @indexCount <= @countNodes
begin
-- get the value from the node
set @nodevalue = (@xml.value('(//RECORD[sql:variable("@indexCount")]/PA0002_NATIO/text())[1]', 'nvarchar(50)'))
-- find in the table the value to be replaced
set @repvalue = (select [Target Code] from [Ronal].[dbo].['Value Mapping$']
where [List Name]='Nationality' and [SAP Code]=@nodevalue)
-- replace the value in the node
set @xml.modify('
replace value of
(//RECORD[sql:variable("@indexCount")]/PA0002_NATIO/text())[1]
with
sql:variable("@repvalue")
');
SET @Indexcount= @Indexcount + 1;
end
end
END
now the idea is to make a generic replace using a variable in the xpath instead of using
set @nodevalue = (@xml.value('(//RECORD[sql:variable("@indexCount")]/PA0002_NATIO/text())[1]', 'nvarchar(50)'))
I would use
set @nodevalue = (@xml.value('(//RECORD[sql:variable("@indexCount")]/[sql:variable("@tag")]/text())[1]', 'nvarchar(50)'))
and of course I would use same syntax to replace
-- replace the value in the node
set @xml.modify('
replace value of
(//RECORD[sql:variable("@indexCount")]/[sql:variable("@tag")]/text())[1]
with
sql:variable("@repvalue")
');
Where the @tag variable contains <PA0002_NATIO> but also <PA0001_CITY> and so on getting the data from another table that store the tag name.
How can I do this?
sql:variableyou can join table you need and usesql:columnselect [Target Code] from [Ronal].[dbo].['Value Mapping$'] where [List Name]='Nationality' and [SAP Code]=@nodevalue