开发者

Modify xml element name in SQL Server

How to change element name from Cust to Customer?

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

When I'm using following statement

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

sql removes attributes

<Cus开发者_如何学Gotomer>
  <Name>aaaaaaaaaa</Name>
  <Desc>bbbbbbbbbb</Desc>
</Customer>


Try this:

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

Gives me an output of:

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

With the FOR XML PATH, you can fairly easily "restore" that attribute that gets lost in the conversion.


You could use replace:

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

This is fairly safe, as < is not valid as data in XML, it would appear as &lt; or an ASCII or UNICODE sequence.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜