Update xml tag in a CLOB column in Oracle

时光毁灭记忆、已成空白 提交于 2019-12-02 04:29:25

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();

After which you end up with:

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>

Or with slightly less scrolling:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!