Hi,
I need to change an xml namespaces. The xml has about 200 elements and every element may have a namespace attribute (namespaces are all the same).
Is there any better solution then the following function?
SQL> create or replace function change_namespace(p_xml xmltype, p_namespace varchar2) return xmltype is
2 l_doc dbms_xmldom.DOMDocument;
3 l_nodes dbms_xmldom.DOMNodelist;
4 l_node dbms_xmldom.DOMNode;
5 begin
6 l_doc:=dbms_xmldom.NewDOMDocument(p_xml);
7 l_nodes:=dbms_xmldom.getElementsByTagName(l_doc,'*');
8 for i in 0..dbms_xmldom.getlength(l_nodes)-1 loop
9 l_node:=dbms_xmldom.item(l_nodes,i);
10 if i=0 then
11 --set namespace only for the root node
12 dbms_xmldom.setattribute(dbms_xmldom.makeElement(l_node),'xmlns',p_namespace);
13 else
14 --remove all the other namespaces
15 dbms_xmldom.removeattribute(dbms_xmldom.makeElement(l_node),'xmlns');
16 end if;
17 end loop;
18
19 return dbms_xmldom.getxmltype(l_doc);
20 end;
21 /
Function created.
SQL> select change_namespace(xmltype('<a xmlns="aaa"><b xmlns="aaa">4</b><c>44</c></a>'),'newnamespace')
2 from dual;
CHANGE_NAMESPACE(XMLTYPE('<AXMLNS="AAA"><BXMLNS="AAA">4</B><C>44</C></A>'),'NEWN
--------------------------------------------------------------------------------
<a xmlns="newnamespace">
<b>4</b>
<c>44</c>
</a>
Ants