Skip to Main Content
  • Questions
  • Oracle XMLUPDATE and XMLQUERY namespace usage difference

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Nama.

Asked: September 30, 2016 - 10:39 am UTC

Last updated: October 06, 2016 - 2:25 pm UTC

Version: Oracle Database 11g Release 11.2.0.4.0 - 64bit Production

Viewed 1000+ times

You Asked

Hi,

I am trying to change the updatexml with xquery way of update, and found the namespace what was used in updatexml cannot be used the same way in the xquery.

Kindly let me know the usage mentioned below is valid or not.

Test code

set serveroutput on

DECLARE
v_xml XMLTYPE;
v_xml_modified xmltype;
begin
v_xml := XMLTYPE(q'[<?xml version = '1.0' encoding = 'UTF-8'?>
<ms:datapacket xmlns:ms=" http://www.abc.com/xyz/ms" xmlns:html=" http://www.w3.org/1999/xhtml" >
<ms:data>
<ms:nonmultirow>
<ms:owner>Smith</ms:owner>
<ms:created_by/>
</ms:nonmultirow>
</ms:data>
</ms:datapacket>]');

SELECT XMLQUERY('declare default element namespace " http://www.abc.com/xyz/ms";
copy $tmp := $p1 modify
(for $i in $tmp/datapacket/data/nonmultirow/owner return replace value of node $i with $lv_owner)
return $tmp'
PASSING V_XML AS "p1",
'John' as "lv_owner"
returning content)
into v_xml_modified
FROM DUAL;
dbms_output.put_line(v_xml_modified.getclobval());

SELECT UPDATEXML(v_xml, 'datapacket/data/nonmultirow/created_by/text()', 'Martin',
'xmlns=" http://www.metricstream.com/appstudio/msa"' )
INTO V_XML_MODIFIED
FROM DUAL;
dbms_output.put_line(v_xml_modified.getclobval());
END;
/



In the above code, for updatexml I am giving the namespace as 'xmlns=" http://www.abc.com/xyz/ms"', and this is working as expected.

For Xquery, I am giving the namespace as default element namespace " http://www.abc.com/xyz/ms"; , and this is working exactly same as updatexml query.

If the namespace(in xmlquery) is given as SELECT XMLQUERY('declare namespace xmlns=" http://www.abc.com/xyz/ms"; then this is not working. Is this the expected behavior?


While migrating from updatexml to XQUERY, do we need to adopt new way we are sending the namespace? is my understanding correct?


Note: My oracle version :11.2.0.4.0

and Chris said...

I'm no XML expert, but looking at the W3C recommendations supported default namespace declarations are in the format:

"declare" "default" ("element" | "function") "namespace"


https://www.w3.org/TR/xquery-30/#id-default-namespace

Meaning

declare namespace


is unsupported.

So... yes?

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here