0

I am trying to generating below format xml using function:

XML

<?xml version="1.0" encoding="UTF-8"?>
<OUTPUT xmlns:xsi="http://www.w3.org">
 ....

Function:

FUNCTION f_get_details(
    p_employee_id NUMBER)
  RETURN XMLTYPE
IS
  v_xmltype xmltype;  
  --  
  main_node xmldom.DOMnode;
  ROOT_NODE XMLDOM.DOMNODE;  
  root_elmt xmldom.domelement;
  l_con_node dbms_xmldom.DOMnode;
  -- 
BEGIN
  l_domdoc := xmldom.newDomDocument; 
  --
  main_node := xmldom.makenode(l_domdoc);
  root_elmt := xmldom.createelement(l_domdoc, 'OUTPUT');
  --
  xmldom.setattribute(elem => root_elmt, name=> 'xmlns:xsi', newvalue=>'http://www.w3.org');
  --
  v_xmltype := dbms_xmldom.getXMLType(l_domdoc);
  dbms_xmldom.freeDocument(l_domdoc); 
  return v_xmltype;
END f_get_details;

function is generating xml in correct format but I am not able to mention namespace of output node. How I should fix it?
I have given namespace in this line of code:

 xmldom.setattribute(elem => root_elmt, name=> 'xmlns:xsi', newvalue=>'http://www.w3.org');

1 Answer 1

1

Answering my own unanswered question, as I have resolved this issue myself. Hope someone will find useful. Below function will add XML namespace now.

FUNCTION f_get_details(
    p_employee_id NUMBER)
  RETURN XMLTYPE
IS
  v_xmltype xmltype;  
  --  
  main_node xmldom.DOMnode;
  ROOT_NODE XMLDOM.DOMNODE;  
  r_elmt xmldom.domelement;
  l_con_node dbms_xmldom.DOMnode;
  -- 
BEGIN
 --Create an empty XML document
  l_domdoc := xmldom.newDomDocument; 
  --
 --XML declaration
  dbms_xmldom.setVersion(l_domdoc, '1.0" encoding="UTF-8');
  dbms_xmldom.setCharset(l_domdoc,'UTF-8');
 --set root node
  r_elmt := xmldom.createelement(l_domdoc, 'test');
  dbms_xmldom.setattribute(r_elmt,'xmlns:xsi','http://www.test.com');
  l_node := dbms_xmldom.appendChild(xmldom.makenode(l_domdoc), dbms_xmldom.makeNode(r_elmt));

  --
  v_xmltype := dbms_xmldom.getXMLType(l_domdoc);
  dbms_xmldom.freeDocument(l_domdoc); 
  /*
  * Rest of coding. 
  */
  return v_xmltype;
END f_get_details;
/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.