views:

171

answers:

2

I'm having trouble parsing an XML response from a web service. I have a feeling this is due to a namespace issue. But, after 4 hours of research, trial-and-error, and head-banging I haven't been able to resolve it. Please help.

My goal is to get a dbms_xmldom.DOMNodeList that contains "ERRORS" nodes.

XML Response:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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"&gt;
<soap:Body>
    <GetMailDataResponse xmlns="https://www.customnamespacehost.com/webservicename"&gt;
        <GetMailDataResult>
            <Errors xmlns="">
                <ErrorDetail>Access Credentials Invalid</ErrorDetail>
            </Errors>
        </GetMailDataResult>
    </GetMailDataResponse>
</soap:Body>
</soap:Envelope>

Code:

Unfortunately this code compiles but doesn't work.

Error: "ORA-31013: Invalid XPATH expression.  

I believe this is due to the multiple namespaces defined in L_NS variable. I tried setting L_XPATH: /soap:Envelope/soap:Body and L_NS: xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" but L_NL_RESULTS ends up being null.

-- Variable Declarations --

P_XML XMLTYPE;

L_CODE_NAME VARCHAR2(1000) := 'PKG_CIS_WS.FNC_STAGE_DATA';
L_XML_DOC                         dbms_xmldom.DOMDocument; 
L_NL_RESULTS                      dbms_xmldom.DOMNodeList;
L_NL_DONOR_SCREENING_RESULTS      dbms_xmldom.DOMNodeList;
L_N_RESULT                        dbms_xmldom.DOMNode;
L_XPATH                           VARCHAR2(4000);
L_NS                              VARCHAR2(4000);
L_TEMP                            VARCHAR2(4000);

-- Code Snippet --   

L_XML_DOC := dbms_xmldom.newDOMDocument(P_XML); 

L_XPATH :=  '/soap:Envelope/soap:Body/a:GetMailDataResponse/GetMailDataResult';
L_NS    :=  'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope"' ||
            'xmlns:a="https://www.customnamespacehost.com/webservicename"';

L_NL_RESULTS := dbms_xslprocessor.selectNodes(
    dbms_xmldom.makeNode(L_XML_DOC)
  , L_XPATH
  , L_NS);

if not DBMS_XMLDOM.ISNULL(L_NL_RESULTS) then

  FOR RESULTS_REC IN 0 .. dbms_xmldom.getLength(L_NL_RESULTS) - 1 LOOP

    L_N_RESULT := dbms_xmldom.item(L_NL_RESULTS, RESULTS_REC);        
    L_TEMP := dbms_xmldom.GETNODENAME(L_N_RESULT);
    prc_bjm(L_CODE_NAME, 'L_TEMP = ' || L_TEMP, SQLCODE);
    dbms_xslprocessor.valueOf(L_N_RESULT, 'Errors/ErrorDetail/text()', L_TEMP);
    prc_bjm(L_CODE_NAME, 'L_TEMP = ' || L_TEMP, SQLCODE);

  END LOOP;

else

  prc_bjm(L_CODE_NAME, 'No nodes for: ' || L_XPATH || '(' || L_NS || ')', SQLCODE);

end if; -- if not DBMS_XMLDOM.ISNULL(L_NL_RESULTS)
A: 

Well, it looks like I found an answer. Probably not the best answer, so please comment if you know of a better (and/or more proper) way!

My solution (again, probably not the best solution) was to really muck around the XPATH query. I added some wild-cards and namespace definitions and things flowed from there.

Working Code

L_XML_DOC := dbms_xmldom.newDOMDocument(P_XML); 

L_XPATH :=  '/soap:Envelope/soap:Body/*[namespace-uri()="https://www.customnamespacehost.com/webservicename"]/*/*[namespace-uri()=""]/*';

L_NL_RESULTS := dbms_xslprocessor.selectNodes(
    dbms_xmldom.makeNode(L_XML_DOC)
  , L_XPATH);

dbms_xmldom.writetobuffer(L_XML_DOC, L_TEMP);
prc_bjm(L_CODE_NAME, 'L_TEMP = ' || L_TEMP, SQLCODE);

if not DBMS_XMLDOM.ISNULL(L_NL_RESULTS) then

  FOR RESULTS_REC IN 0 .. dbms_xmldom.getLength(L_NL_RESULTS) - 1 LOOP

    L_N_RESULT := dbms_xmldom.item(L_NL_RESULTS, RESULTS_REC); 

    L_TEMP := dbms_xmldom.GETNODENAME(L_N_RESULT);

    prc_bjm(L_CODE_NAME, 'L_TEMP = ' || L_TEMP, SQLCODE);

    dbms_xslprocessor.valueOf(L_N_RESULT, 'text()', L_TEMP);

    prc_bjm(L_CODE_NAME, 'L_TEMP = ' || L_TEMP, SQLCODE);

  END LOOP;

else

  prc_bjm(L_CODE_NAME, 'No nodes for: ' || L_XPATH || '(' || L_NS || ')', SQLCODE);

end if; -- if not DBMS_XMLDOM.ISNULL(L_NL_RESULTS)

Also, please comment if you know why this way works. It really seems strange to me that the namespace (for the GetMailDataResponse node) is being defined without an alias (example: "soap.") I imagine that my original problem would go away if the GetMailDataResponse node was defined as follows:

<a:GetMailDataResponse xmlns:a="https://www.customnamespacehost.com/webservicename"&gt;  
zechariahs
A: 

Here is an example in PL/SQL that pulls those values out in a different way. One thing I noticed from your XML snippet you posted was you were missing a closing </soap:Envelope>

It is a self-contained example you can run in SQL plus and see the output from the put_line calls.

set serveroutput on

declare
 v_xml xmltype;
 v_str varchar2(3000);
 v_temp varchar2(1000);
begin

 v_str := '<?xml version="1.0" encoding="ISO-8859-1"?>' ||
          '<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"&gt;' ||
          '<soap:Body>' ||
          '    <GetMailDataResponse xmlns="https://www.customnamespacehost.com/webservicename"&gt;' ||
          '        <GetMailDataResult>' ||
          '            <Errors xmlns="">' ||
          '                <ErrorDetail>Access Credentials Invalid</ErrorDetail>' ||
          '                <ErrorDetail>Foobar</ErrorDetail>' ||
          '            </Errors>' ||
          '        </GetMailDataResult>' ||
          '    </GetMailDataResponse>' ||
          '</soap:Body></soap:Envelope>';

 --load our string into an xmltype variable    
 v_xml := xmltype(v_str);

 --loop through each ErrorDetail entry
 for rec in (select value(x) txt
               from table(XMLSequence(extract(v_xml, '//Errors/ErrorDetail'))) x
             )
 loop

    --output the XML that was returned
    dbms_output.put_line('xml_snippet: ' || rec.txt.getStringVal());

    --pull the value out of that XML
    select extractvalue(rec.txt, '//*')
      into v_temp
      from dual;

   --display the value that was pulled out
   dbms_output.put_line('xml_vlaue: ' || v_temp);

  end loop;

end;
Dougman
Thanks for pointing out the missing tag Dougman. It's in my code, I just forgot to copy it over.
zechariahs