【发布时间】:2018-06-15 22:22:23
【问题描述】:
我正在尝试更新 XML 列中的数据,并添加一个新属性(如果它尚不存在)。
这是我的脚本中最相关的部分:
UPDATE sp
set [PolicyXML].modify
('insert attribute source {sql:variable("@source")}
into (/policies/policy[@type=sql:variable("@policyType")]/groups/group[@id=sql:variable("@groupId") and @accessPermissions=sql:variable("@accessPermissions") and @domainId=sql:variable("@domainId") and not (@source)])[1]
')
FROM #spTemp as sp
更改后,有问题的数据变为:
<policies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<policy type="default">
<groups>
<group id="00000000-0000-0000-0000-000000000000" source="ZZ_MYNEWVALUE_ZZ" accessPermissions="31" domainId="00000000-0000-0000-0000-000000000000" />
</groups>
</policy>
</policies>
但我需要的是:
<policies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<policy type="default">
<groups>
<group id="00000000-0000-0000-0000-000000000000" accessPermissions="31" domainId="00000000-0000-0000-0000-000000000000" source="ZZ_MYNEWVALUE_ZZ" />
</groups>
</policy>
</policies>
我似乎可以在互联网上找到的每篇文章都在谈论使用“as last into”而不是“into”,但这似乎是为了让节点成为最后一个节点,而不是让属性成为最后一个属性。这里没有效果。
有人知道如何让这个新属性排在最后吗?
【问题讨论】:
-
根据this specification,属性的顺序不重要(而元素的顺序是)。即使您设法将属性放入给定的位置,下次打开它时,它也可能会出现在其他地方......
-
这似乎是对的。我不确定如果我们使用模式定义是否仍然如此,但在这种情况下似乎也对 order 无动于衷。谢谢。
标签: sql-server xml tsql xpath