使用 Xquery 替换 xsi:nil = "true" 的节点值

发布于 2024-10-07 07:47:22 字数 808 浏览 13 评论 0 原文

我正在尝试使用 SQL Server 2005 中的 XQuery 来更新保存在列中的 xml。这是我需要更新的数据示例。

<Length>3</Length>
<Width>5</Width>
<Depth>6</Depth>
<Area xsi:nil="true" />
<Volume xsi:nil="true" />

我需要将面积和体积设置为不同表中的值。我正在为更新创建 CTE。我省略了其他逻辑,但我已经验证 CTE 包含用于更新的正确数据:

;with Volume (DocumentID, Volume) As
(
  Select DocumentID, Volume from tbl
)

并且我正在使用以下 XQuery SQL 语句来尝试更新表。

UPDATE tbl_Archive
   SET XML.modify(' declare namespace x="http://www.redacted.com";
   replace value of  (/x:Document/x:Volume/text())[1]
   with sql:column("Volume.Volume")')
    From Volume where volume.documentID = tbl_Archive.DocumentID

我有 1 行受到影响,但当我查看 XML 时,它没有改变,而且我不知道需要修复什么才能使其正常工作。如果有什么区别的话,该节点是无类型的。

I am trying to use XQuery in SQL Server 2005 to update xml saved in a column. Here is a sample of the data I need to update.

<Length>3</Length>
<Width>5</Width>
<Depth>6</Depth>
<Area xsi:nil="true" />
<Volume xsi:nil="true" />

I need to set the area and volume to values from a different table. I am creating a CTE for the update. There is other logic that I have omitted, but I have verified that the CTE contains the correct data for the update:

;with Volume (DocumentID, Volume) As
(
  Select DocumentID, Volume from tbl
)

and I am using the following XQuery SQL statement to try to update the table.

UPDATE tbl_Archive
   SET XML.modify(' declare namespace x="http://www.redacted.com";
   replace value of  (/x:Document/x:Volume/text())[1]
   with sql:column("Volume.Volume")')
    From Volume where volume.documentID = tbl_Archive.DocumentID

I get 1 row affected, but when I look at the XML it hasn't changed, and I can't figure out what needs to be fixed to make it work. The node is untyped, if that makes any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2024-10-14 07:47:22

如果没有要替换的文本,更新将不起作用。XPath /x:Document/x:Volume/text())[1] 将返回一个空集。

尝试插入.....

UPDATE tbl_Archive
   SET XML.modify(' declare namespace x="http://www.redacted.com";
   insert text {sql:column("Volume.Volume")}
   as first into  (/x:Document/x:Volume)[1]')
    From Volume where volume.documentID = tbl_Archive.DocumentID

然后您需要删除 nil="true" 属性..

也许是这样的..

 update tbl_Archive set XML.modify('delete /*:Document/*:Volume[text()]/@xsi:nil')

Update wont work if there's no text to replace.. the XPath /x:Document/x:Volume/text())[1] will return an empty set.

Try insert...

UPDATE tbl_Archive
   SET XML.modify(' declare namespace x="http://www.redacted.com";
   insert text {sql:column("Volume.Volume")}
   as first into  (/x:Document/x:Volume)[1]')
    From Volume where volume.documentID = tbl_Archive.DocumentID

..you'll then need to remove the nil="true" attribute..

Something like this maybe..

 update tbl_Archive set XML.modify('delete /*:Document/*:Volume[text()]/@xsi:nil')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文