如何在 SQL Server 中重命名 XML 节点名称 [英] How to rename XML node name in a SQL Server

查看:59
本文介绍了如何在 SQL Server 中重命名 XML 节点名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一张表,其中一列存储 XML 数据.由于源代码的变化,我们想重命名一个特定的 XML 节点名称和 XML 命名空间.假设我有以下 XML:

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>

如何将节点 MediaClass 名称重命名为 Book 并更改命名空间值?

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

所以它看起来像:

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

我需要完全在 T-SQL 中完成,因为这将用作迁移脚本.我们客户上安装的最低 SQL Server 是 SQL Server 2005.

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.

推荐答案

我不知道 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>')

这篇关于如何在 SQL Server 中重命名 XML 节点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆