【问题标题】:how to declare namespace when using sql variable to insert multiple xml nodes使用sql变量插入多个xml节点时如何声明命名空间
【发布时间】:2011-10-05 00:11:59
【问题描述】:

我正在尝试将@newLinks 插入@links 但不确定如何在以下sql 中声明前缀“xsi”:

declare @links xml
set @links = N'<Links/>';

declare @newLinks xml
set @newLinks = N'
<Link xsi:type="CustomLink">
  <Name>Foo</Name>
</Link>
<Link xsi:type="CustomLink">
  <Name>Bar</Name>
</Link>';

set @links.modify('
insert sql:variable("@newLinks")
into (/Links)[1]');

select @links;

执行上面的sql会出现如下错误:

XML parsing: line 2, character 28, undeclared prefix

我尝试了以下方法,但没有成功:

set @links.modify('
declare namespace xsi="http://www.w3.org/2001/XMLSchema-instance";
insert sql:variable("@newLinks")
into (/Links)[1]');

【问题讨论】:

    标签: sql-server xml xml-dml


    【解决方案1】:

    您可以在 xml 片段中使用 xmlns 属性来定义 xsi 前缀。该声明适用于所有子节点。您的片段中没有根节点,因此您必须在两个 &lt;link/&gt; 元素上定义它。

    declare @newLinks xml
    set @newLinks = N'
    <Link 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:type="CustomLink">
      <Name>Foo</Name>
    </Link>
    <Link 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:type="CustomLink">
      <Name>Bar</Name>
    </Link>';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      相关资源
      最近更新 更多