Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse xml by xmltable when using namespace in xml(Oracle)

Tags:

xml

oracle

plsql

I want to parse a xml string that is a web service response sent from servier, the xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <addResponse xmlns="http://tempuri.org/">
            <addResult>20</addResult>
        </addResponse>
    </soap:Body>
</soap:Envelope>

I want to get the value 20 between elements addResult. My plsql code segment looks like following:

declare
  v_xml clob;
begin
  v_xml := '<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <addResponse xmlns="http://tempuri.org/">
        <addResult>20</addResult>
      </addResponse>
    </soap:Body>
  </soap:Envelope>';
  for c in (select results 
        from xmltable('Envelope/Body/addResponse' passing xmltype(v_xml) 
        columns results varchar(100) path './addResult')
       )
  loop
    dbms_output.put_line('the result of calculation is : ' || c.results);
  end loop;
end;

seems that nothing was printed out, but if I remove the namespace 'soap', the code works well, so can anybody tell me how can I got the value 20 when the xml has namespace?

like image 990
Zhe Xin Avatar asked Mar 06 '14 08:03

Zhe Xin


People also ask

What is Xmltable?

XMLTABLE is a SQL/XML function that is implemented in the FROM clause of a SQL but in combination with a driving table that contains the XML data for translating the XML data to a relational form. Hence, using this Oracle XMLTABLE function one can retrieve several information from the XML data. Syntax.

What is Oracle XMLType?

XMLType is a system-defined opaque type for handling XML data. It as predefined member functions on it to extract XML nodes and fragments. You can create columns of XMLType and insert XML documents into it.

What is parsing XML data?

Definition. XML parsing is the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.


1 Answers

Based on this answer

Should be like this:

declare    
  v_xml clob;    
begin    
  v_xml := '<?xml version="1.0" encoding="utf-8"?>    
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">    
    <soap:Body>    
      <addResponse xmlns="http://tempuri.org/">    
        <addResult>20</addResult>    
      </addResponse>    
    </soap:Body>    
  </soap:Envelope>';    
  for c in (select results    
              from xmltable(xmlnamespaces(default 'http://tempuri.org/',    
                                          'http://schemas.xmlsoap.org/soap/envelope/' as    
                                          "soap" ),    
                            'soap:Envelope/soap:Body/addResponse' passing    
                            xmltype(v_xml) columns results varchar(100) path    
                            './addResult')) loop    
    dbms_output.put_line('the result of calculation is : ' || c.results);    
  end loop;    
end;
like image 79
A.B.Cade Avatar answered Nov 15 '22 05:11

A.B.Cade