开发者

How to rename XML node name in a SQL Server

I have on my database a table with one column storing XML data. Due to changes in the source code we want to rename one specific XML node name and XML namespace. Lets say that I have the XML bellow:

<MediaClass xmlns="MediaClass/1">   
    <Media>
        <Title>Test</Title>
        <Type>Book</Type>
        <Price>1.00</Price>
    </Media>
</MediaClass>

How can I rename the Node MediaClass name to let's say Book and also change the namespace value?

So it can 开发者_StackOverflow社区look like as:

<Book xmlns="Book/1">   
    <Media>
        <Title>Test</Title>
        <Type>Book</Type>
        <Price>1.00</Price>
    </Media>
</Book>

I need to do it entirely in T-SQL as this will be used as a migration script. The minimum SQL Server installed on ours customers is SQL Server 2005.


I don't know if this is possible with XML DML

It might work for you to use replace instead.

update YourTable set
  XMLCol = replace(replace(cast(XMLCol as nvarchar(max)), 
                           '<MediaClass xmlns="MediaClass/1">', 
                           '<Book xmlns="Book/1">'), 
                   '</MediaClass>', 
                   '</Book>')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜