I have a XML column in a SQL Server table. I want to add an attribute to the nodes present in the XML. That attribute will be a unique identifier to the nodes.
I have written some code like:
DECLARE @i int, @key varchar(20);
SET @key = 'key'
SET @i = 1
IF (@i < 10)
SET @key = @key + @i
UPDATE copy
SET dataxml.modify('insert attribute key {@key}
into (/Package[1]/Section/*[local-name() = ("List", "InputNumber", "InputText")][@i])[1]')
WHERE id = 10;
I am not sure how to loop over this XML and how exactly to add the attribute to the correct node because it has nodes like List, InputNumber, InputText in random order.
The XML looks like:
<Package>
<Section name='BOX'>
<InputNumber description="[1] What is the height of the Box.">
<value/>
</InputNumber>
<InputNumber description="[2] What is the width of the Box.">
<value/>
</InputNumber>
<List description="[3] What is the type of BOX.">
<option/>
</List>
</Section>
<Section name='Cover'>
<InputText description="[4] Color of cover.">
<value/>
</InputText>
<List description="[5] Type of cover.">
<option/>
</List>
</Section>
</Package>
I want the output to be like:
<Package>
<Section name='BOX'>
<InputNumber key="key1" description="[1] What is the height of the Box.">
<value/>
</InputNumber>
<InputNumber key="key2" description="[2] What is the width of the Box.">
<value/>
</InputNumber>
<List key="key3" description="[3] What is the type of BOX.">
<option/>
</List>
</Section>
<Section name='Cover'>
<InputText key="key4" description="[4] Color of cover.">
<value/>
</InputText>
<List key="key5" description="[5] Type of cover.">
<option/>
</List>
</Section>
</Package>
I am getting error while running the mentioned query:
XQuery [copy.DataXML.modify()]: Top-level attribute nodes are not supported