Oracle Xpath

Experts... How do I replace the value in an element of xmltype.  ex: I have an xmltype and which has the following structure
      <fstype> 2 </fstype>

If I want to replace the element <arp> with a value of 5 after the xml is built how do I do it.
I saw that there isa function UpdateXML in Oracle. Not sure how to use it.
Who is Participating?
slightwv (䄆 Netminder) Commented:
>>will the updatexml statement throw an error


In the future, just try it.  Take my quick simple test case and remove the arp node and try it.
slightwv (䄆 Netminder) Commented:
try this
drop table tab1 purge;
create table tab1(col1 xmltype);

insert into tab1 values(xmltype('<decsn> <fs> <fstype> 2 </fstype> <fscmp>abc</fscmp> <arp>3.4</arp> </fs> </decsn>'));

UPDATE tab1 SET col1 = UPDATEXML(col1, '/decsn/fs/arp/text()', '5');

select * from tab1;

Open in new window

chickannaAuthor Commented:
slightwv, I have the xml in a variable say v_xml. So I use the same 'UPDATEXML' statement, except in place of col1 use v_xml??
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

slightwv (䄆 Netminder) Commented:
just replace the variable name with the column name:

v_xml := UPDATEXML(v_xml, '/decsn/fs/arp/text()', '5');

yes, and it's not an update statement,  you simply do an assignment

v_xml := updatexml(v_xml,....
slightwv (䄆 Netminder) Commented:

depending on your version you might have to 'select' it since UPDATEXML is not a PL/SQL function.

see here:

	v_xml xmltype := xmltype('<decsn> <fs> <fstype> 2 </fstype> <fscmp>abc</fscmp> <arp>3.4</arp> </fs> </decsn>');


select UPDATEXML(v_xml, '/decsn/fs/arp/text()', '5') into v_xml from dual;


Open in new window

ah, good catch

slightwv is correct,  BOTH of our direct assignment posts were wrong.

updatexml isn't available as pl/sql function in any version up through 11gR2.
perhaps someday when 12 comes out but not for now
chickannaAuthor Commented:
Thanks. One last twist in this.. If in the xmltype variable if the element 'arp' does not exist, will the updatexml statement throw an error. I have a situation where the element may not exist.
chickannaAuthor Commented:
Thanks a lot.
chickannaAuthor Commented:
slightwv, I created a function where in I pass the xml and am trying to update it within the function. It compiled fine but when I try to run it in an anonymous block I get this error
ORA-30625: method dispatch on NULL SELF argument is disallowed

here is my code

I have this xml in a variable v_xml

I pass it to a function

get_dtls_fn(i_xml xmltype)
return xmltype
v_xml2 xmltype;
select UPDATEXML(i_xml, '/decsn/fs/arp/text()', '5') into v_xml2 from dual;
return v_xml2;

and when I call this function from an anonymous block like

set serveroutput on;
v_clob clob:=null;
v_xml1 xmltype;
-- crdt_aplcn_oid,rte_one_fnc_src_id,crdt_dcsn_oid
v_xml1:=get_dtls_fn(v_xml);  -- v_xml I get if from another function call and this is a valid xml
v_clob := v_xml.getClobVal();
v_num:= 1;
dbms_output.put_line('vxml' || v_clob);

I get the following error ORA-30625: method dispatch on NULL SELF argument is disallowed

what is the solution here
I think the problem is somewhere external to the snippet you have posted
if you want to pursue your function that should be a new question
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.