本文介绍了更新 Oracle 中 CLOB 列中的 xml 标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Oracle 11g 的 CLOB 列中有这个 xml 值:

I have this xml value in a CLOB column in Oracle 11g:

<Energy xmlns="http://euroconsumers.org/notifications/2009/01/notification">
    <Gender>M</Gender>
    <FirstName>MAR</FirstName>
    <Name>VAN HALL</Name>
    <Email/><Telephone>000000000</Telephone>
    <InsertDate>2013-10-09</InsertDate>
</Energy>

我想更新几行的 InserDate 值.

I want to update the value of InserDate for several rows.

我正在使用下面的 sql 命令:

I was using next below sql command:

update tmp_tab_noemail_test p1
set p1.sce_msg = updatexml(xmltype(p1.sce_msg),
                 '//Energy/InsertDate/text()','Not Valid').getClobVal()

但是不起作用.

你有什么想法只修改InsertDate的xml标签的值吗?

Do you have some ideas to modify only the values of the xml tag of InsertDate?

提前感谢

推荐答案

您的顶级 Energy 节点中有一个命名空间,因此您无法匹配;UPDATEXML 文档 显示您可以选择提供命名空间字符串.

You have a namespace in your top-level Energy node, so you aren't matching without; the UPDATEXML documentation shows you can optionally supply a namespace string.

因此您可以使用示例数据执行此操作:

So you can do this, using your example data:

create table tmp_tab_noemail_test (sce_msg clob);
insert into tmp_tab_noemail_test values (
'<Energy xmlns="http://euroconsumers.org/notifications/2009/01/notification">
    <Gender>M</Gender>
    <FirstName>MAR</FirstName>
    <Name>VAN HALL</Name>
    <Email/><Telephone>000000000</Telephone>
    <InsertDate>2013-10-09</InsertDate>
</Energy>');

update tmp_tab_noemail_test p1
set p1.sce_msg = updatexml(xmltype(p1.sce_msg),
  '/Energy/InsertDate/text()','Not Valid',
  'xmlns="http://euroconsumers.org/notifications/2009/01/notification"').getClobVal();

之后你会得到:

select sce_msg from tmp_tab_noemail_test;

SCE_MSG
--------------------------------------------------------------------------------
<Energy xmlns="http://euroconsumers.org/notifications/2009/01/notification"><Gender>M</Gender><FirstName>MAR</FirstName><Name>VAN HALL</Name><Email/><Telephone>000000000</Telephone><InsertDate>Not Valid</InsertDate></Energy>

或者稍微减少滚动:

select XMLQuery('//*:InsertDate' passing XMLType(sce_msg) returning content) as insertdate
from tmp_tab_noemail_test;

INSERTDATE
--------------------------------------------------------------------------------
<InsertDate xmlns="http://euroconsumers.org/notifications/2009/01/notification">Not Valid</InsertDate>

您也可以通配更新:


You could also wildcard the update:

update tmp_tab_noemail_test p1
set p1.sce_msg = updatexml(xmltype(p1.sce_msg),
  '/*:Energy/*:InsertDate/text()','Not Valid').getClobVal();

...但最好指定命名空间.

... but it's probably better to specify the namespace.

这篇关于更新 Oracle 中 CLOB 列中的 xml 标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:59