更新 XMLType 向节点添加新属性

Update XMLType adding new attribute to the node

我们在从 Oracle 10 迁移到 Oracle 12c 的过程中遇到了困难。 我们已经迁移了部分数据库,其中大部分数据包含 XmlType 文档。由于新要求,部分文档需要更新属性。

最初,我们有以下 xml:

<root>
 <a id="1">
  aaaaaaaaaa
 </a>
 <b id="2">
  bbbbbbbbbb
 </b>
</root>

xml更新后,需要变成这样:

<root>
 <a id="1">
  aaaaaaaaaa
 </a>
 <b id="2" newAttribute="">
  bbbbbbbbbb
 </b>
</root>

在 Oracle 10 中,这会成功:

update TABLE_NAME set whattoUpdate = (select insertchildxml(whattoUpdate, 'xpathexpression', '@attribute', 'valueOfAttribute', 'namespace') as result  from TABLE_NAME where id = XX);

这可以正常工作,但在 Oracle 12 中它不起作用(select 语句 returns 空白 xml 在 SqlDeveloper 中)。

根据 this,不再支持函数 insertchildxml()

create table example( xml clob);

insert into example values( '<root>
 <a id="1">
  aaaaaaaaaa
 </a>
 <b id="2">
  bbbbbbbbbb
 </b>
</root>');

update example set xml = xmlserialize( document XMLQuery('
xquery version "1.0";
copy $cp := .
modify
    insert node attribute  newAttribute {""} into $cp/root/a
return $cp
'
passing xmltype(xml) returning content) indent size = 2);

xmlserialize(document {xmltype} indent size =2 ) - 正在将 xml 更改为 clob。
xmluqery结构是 xmlquery( 'xquery language' passing {xmltype} returning content)

copy $cp :=. 创建输入 xml 文档的副本。必填,因为无法修改原始数据。

modify insert node attribute newAttribute {""} into $cp/root/a return $cp- xquery 命令。在所选元素中添加新属性。