F0cus
F0cus

Reputation: 625

Remove a namespace from XML using Oracle SQL 11g

I am using Oracle 11g and I try to create XML data. One of the tags is:

<majorLine>

I use the below code to generate it:

l_major_line := DBMS_XMLDOM.createElement (l_domdoc, 'ns1:majorLine');  

DBMS_XMLDOM.setattribute (l_major_line, 'xmlns:ns1','urn:xyz:ccw:config:msa:data');
DBMS_XMLDOM.setattribute (l_major_line, 'lineId', l_line_id);

It gives this output:

<ns1:majorLine xmlns:ns1="urn:xyz:ccw:config:msa:data"  linId = "12345">

The issue is, that I don't want the portion: "xmlns:ns1="urn:xyz:ccw:config:msa:data"

Instead I need:

<ns1:majorLine linId = "12345">

Please help. Thanks in advance.

Upvotes: 2

Views: 3597

Answers (1)

Qualtagh
Qualtagh

Reputation: 721

Use dbms_xmldom.removeAttribute:

declare
  l_domdoc dbms_xmldom.DOMDocument;
  l_major_line dbms_xmldom.DOMElement;
  l_node dbms_xmldom.DOMNode;
  l_attrs dbms_xmldom.DOMNamedNodeMap;
  l_len pls_integer;
  l_name varchar2( 4000 );
  l_clob clob;
begin
  -- Sample data preparation.
  l_domdoc := dbms_xmldom.createDocument( 'xmlns:ns1', 'doc' );
  l_major_line := dbms_xmldom.createElement( l_domdoc, 'ns1:majorLine' );
  dbms_xmldom.setAttribute( l_major_line, 'lineId', 12345 );
  dbms_xmldom.setAttribute( l_major_line, 'xmlns:ns1', 'urn:xyz:ccw:config:msa:data' );

  -- Remove exact namespace.
  --dbms_xmldom.removeAttribute( l_major_line, 'xmlns:ns1' );

  -- Or remove generic namespace if the exact name is not known.
  l_node := dbms_xmldom.makeNode( l_major_line );
  l_attrs := dbms_xmldom.getAttributes( l_node );
  l_len := dbms_xmldom.getLength( l_attrs );
  for i in 1 .. l_len loop
    l_node := dbms_xmldom.item( l_attrs, i );
    l_name := dbms_xmldom.getNodeName( l_node );
    if lower( l_name ) like 'xmlns:%' then
      dbms_xmldom.removeAttribute( l_major_line, l_name );
    end if;
  end loop;

  -- Output.
  l_node := dbms_xmldom.makeNode( l_major_line );
  dbms_lob.createTemporary( l_clob, true );
  dbms_xmldom.writeToClob( l_node, l_clob );
  dbms_output.put_line( l_clob );
end;

Upvotes: 1

Related Questions