如何在 SQL 服务器中添加或更改 Xml 名称空间/不能在名称表达式中使用 'xmlns'
How to add or change an Xml namespace in SQL Server / Cannot use 'xmlns' in the name expression
我有一个 XML 列不是用名称空间生成的,这意味着没有 xmlns 属性。不幸的是,我无法解决实际问题,即创建 XML 的位置。
例如:
<root>Our Content</root>
我 可以 修改 XML 数据,然后再将其返回到需要命名空间的特定客户端。我想要的很简单:
<root xmlns="http://OurNamespace">Our Content</root>
我试过类似的方法:
.modify('insert attribute xmlns {"ournamespace"}...
但是
的错误
Cannot use 'xmlns' in the name expression.
我的问题是:
- 是否有解决此特定错误的技术?
- add/change SQL XML 类型的命名空间是否有替代或更好的方法?
这是在 SQL Server 2012 存储过程中。
也许就这么简单?
DECLARE @xml XML='<root>Our Content</root>';
SELECT CAST( REPLACE(CAST(@xml AS NVARCHAR(MAX)),'<root>','<root xmlns="http://OurNamespace">') AS XML)
我能想出的最佳选择,select 个根子节点并将其放在 <root xmlns="http://OurNameSpace">...</root>
.
之间
DECLARE @t TABLE(e XML);
INSERT INTO @t(e)VALUES('<root><el1>Our Content</el1></root>');
INSERT INTO @t(e)VALUES('<root><el2>Our Content</el2></root>');
SELECT
'<root xmlns="http://OurNameSpace">'+
CAST(e.query('/root/*') AS NVARCHAR(MAX))+
'</root>'
FROM @t;
我最接近 XQuery 的是:
SELECT e.query('<root xmlns="http://OurNameSpace">{*:root/*}</root>')
FROM @t;
但是 select 是具有 xmlns=""
(<el1 xmlns="">
) 的第一个子元素(例如 <el1>
)。我没有找到删除它的方法。但也许这对你来说已经足够了?
虽然我还在研究这个,但我正在使用
insert attribute xmlns {"http://www.ms.com"} into (/root)[1]
会抛出错误
Cannot use 'xmlns' in the name expression of computed attribute
constructor.
本文描述了这样做的原因:
Adding-xmlns-to-root-element
总而言之,xmlns 不应在现有 XML 结构中更改,因为在以下可能的实施情况下 Microsoft Forum: SQL XML Namespace Issue 建议所有 现有的子元素也必须更改 。
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="uri">
<ns1:ProductID>316</ns1:ProductID>
<ns1:Name>Blade</ns1:Name>
<ns1:Color xsi:nil="true" />
</row>
我有一个 XML 列不是用名称空间生成的,这意味着没有 xmlns 属性。不幸的是,我无法解决实际问题,即创建 XML 的位置。
例如:
<root>Our Content</root>
我 可以 修改 XML 数据,然后再将其返回到需要命名空间的特定客户端。我想要的很简单:
<root xmlns="http://OurNamespace">Our Content</root>
我试过类似的方法:
.modify('insert attribute xmlns {"ournamespace"}...
但是
的错误Cannot use 'xmlns' in the name expression.
我的问题是:
- 是否有解决此特定错误的技术?
- add/change SQL XML 类型的命名空间是否有替代或更好的方法?
这是在 SQL Server 2012 存储过程中。
也许就这么简单?
DECLARE @xml XML='<root>Our Content</root>';
SELECT CAST( REPLACE(CAST(@xml AS NVARCHAR(MAX)),'<root>','<root xmlns="http://OurNamespace">') AS XML)
我能想出的最佳选择,select 个根子节点并将其放在 <root xmlns="http://OurNameSpace">...</root>
.
DECLARE @t TABLE(e XML);
INSERT INTO @t(e)VALUES('<root><el1>Our Content</el1></root>');
INSERT INTO @t(e)VALUES('<root><el2>Our Content</el2></root>');
SELECT
'<root xmlns="http://OurNameSpace">'+
CAST(e.query('/root/*') AS NVARCHAR(MAX))+
'</root>'
FROM @t;
我最接近 XQuery 的是:
SELECT e.query('<root xmlns="http://OurNameSpace">{*:root/*}</root>')
FROM @t;
但是 select 是具有 xmlns=""
(<el1 xmlns="">
) 的第一个子元素(例如 <el1>
)。我没有找到删除它的方法。但也许这对你来说已经足够了?
虽然我还在研究这个,但我正在使用
insert attribute xmlns {"http://www.ms.com"} into (/root)[1]
会抛出错误
Cannot use 'xmlns' in the name expression of computed attribute constructor.
本文描述了这样做的原因: Adding-xmlns-to-root-element
总而言之,xmlns 不应在现有 XML 结构中更改,因为在以下可能的实施情况下 Microsoft Forum: SQL XML Namespace Issue 建议所有 现有的子元素也必须更改 。
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="uri">
<ns1:ProductID>316</ns1:ProductID>
<ns1:Name>Blade</ns1:Name>
<ns1:Color xsi:nil="true" />
</row>