3

尝试更新包含在变量中的 XML 中的一个属性:

set @x.modify('declare namespace abc="http://abcsystems.com/";
replace value of
(/abc:App/abc:Client/abc:Addresses/abc:PostalAddress)[@AddressLine1] with "555 Service Rd."')

在 .上尝试了有和没有下标@AddressLine1[1]

这会引发错误:

Msg 2337, Level 16, State 1, Line 8
XQuery [modify()]: 'replace' 的目标最多只能是一个节点,找到'element(abc{ http://abcsystems.com/ }:PostalAddress,xdt :无类型) *'

PostalAddress整个 XML中只有一个元素。错误告诉我什么?

4

1 回答 1

2

如果没有实际的 XML,这将是盲目的,但您可能正在寻找这个:

set @x.modify('declare namespace abc="http://abcsystems.com/";
replace value of
(/abc:App/abc:Client/abc:Addresses/abc:PostalAddress/@AddressLine1)[1] with "555 Service Rd."')

通常(xpath here)[1]用于强制执行单个节点

更新:工作示例

declare @x xml=
N'<abc:App xmlns:abc="http://abcsystems.com/">
  <abc:Client>
    <abc:Addresses>
      <abc:PostalAddress AddressLine1="test" />
    </abc:Addresses>
  </abc:Client>
</abc:App>';

set @x.modify('declare namespace abc="http://abcsystems.com/";
replace value of
(/abc:App/abc:Client/abc:Addresses/abc:PostalAddress/@AddressLine1)[1] 
with "555 Service Rd."');

select @x;

结果

<abc:App xmlns:abc="http://abcsystems.com/">
  <abc:Client>
    <abc:Addresses>
      <abc:PostalAddress AddressLine1="555 Service Rd." />
    </abc:Addresses>
  </abc:Client>
</abc:App>
于 2016-11-29T17:20:22.777 回答