aaaaaaaaaa bb...">

Changing the name of an XML element in SQL Server

How to change item name from Cust to client?

<Cust id="1">
  <Name>aaaaaaaaaa</Name>
  <Desc>bbbbbbbbbb</Desc>
</Cust>

When I use the following instruction

select @myXml.query('/node()[1]/node()') for xml raw('Customer')

sql removes attributes

<Customer>
  <Name>aaaaaaaaaa</Name>
  <Desc>bbbbbbbbbb</Desc>
</Customer>
+3
source share
2 answers

Try the following:

SELECT
    @myXml.value('(/Cust/@id)[1]', 'int') AS '@id',
    @myXml.query('/node()[1]/node()') 
FOR XML PATH('Customer')

Gives me the conclusion:

<Customer id="1">
  <Name>aaaaaaaaaa</Name>
  <Desc>bbbbbbbbbb</Desc>
</Customer>

With the help of FOR XML PATHyou can quite easily "restore" this attribute, which will be lost during the conversion.

+1
source

You can use replace:

replace(replace(@YourXml, '<Cust id', '<Customer id)', '</Cust>', '</Customer>')

This is pretty safe because it is <invalid as data in XML, it will look like &lt;either an ASCII or UNICODE sequence.

+1
source

Source: https://habr.com/ru/post/1794740/


All Articles