使用Oracle SQL 11g从XML中删除命名空间

时间:2015-03-30 19:11:23

标签: xml oracle oracle11g

我使用的是Oracle 11g,我尝试创建XML数据。其中一个标签是:

<majorLine>

我使用以下代码生成它:

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

它给出了这个输出:

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

问题是,我不想要这个部分:“xmlns:ns1 =”urn:xyz:ccw:config:msa:data“

相反,我需要:

<ns1:majorLine linId = "12345">

请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用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;