2016-03-02 12 views
0

當我嘗試解析XML文檔時,我在這裏遇到了XPath屬性的小問題。 這是我的例子:如何在Oracle中通過xmltype解析XML

DECLARE 
    px_return XMLTYPE 
     := XMLTYPE (
      '<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP:Header xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"> 
     <h:AxisValues xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:h="urn:/microsoft/multichannelframework/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:/microsoft/multichannelframework/"> 
      <User xmlns="">FSCD</User> 
      <Solution xmlns="">Multicare</Solution> 
      <ApplicationalUser xmlns=""/> 
      <ApplicationalUserSystem xmlns=""/> 
      <SystemUser xmlns=""/> 
      <SystemUserSystem xmlns=""/> 
      <Proxy xmlns="">0</Proxy> 
     </h:AxisValues> 
    </SOAP:Header> 
    <SOAP:Body xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"> 
     <ns1:maintainMandateResponse xmlns:ns1="urn:enterprise.com/ws/SAP/Finantial/MaintainMandate/V1"> 
      <return> 
       <messageType>E</messageType> 
      </return> 
     </ns1:maintainMandateResponse> 
    </SOAP:Body> 
</soapenv:Envelope>'); 

    lv_msgType VARCHAR2 (20); 
BEGIN 
    SELECT Return.msgType 
    INTO lv_msgType 
    FROM XMLTABLE (
      xmlnamespaces (
       DEFAULT 'enterprise.com/ws/SAP/Finantial/MaintainMandate/V1', 
       'http://schemas.xmlsoap.org/soap/envelope/' AS "soapenv", 
       'http://schemas.xmlsoap.org/soap/envelope/' AS "SOAP", 
       'enterprise.com/ws/SAP/Finantial/MaintainMandate/V1' AS "ns1"), 
      '//soapenv:Envelope/SOAP:Body/ns1:maintainMandateResponse' 
      PASSING px_return 
      COLUMNS msgType VARCHAR2 (1) PATH 'messageType') Return; 


    DBMS_OUTPUT.put_line ('Message type: ' || lv_msgType); 
END; 

我得到一個NO_DATA_FOUND異常,因爲我無法找到在此分析方法的結果。

我已經嘗試了很多不同的策略,包括將return放在PATH或XQUery字符串中,但沒有成功。

我認爲這是一個小而簡單的問題,但我無法找到。 在此先感謝! Filipe

回答

1

您在ns1名稱空間聲明中缺少urn:前綴。您也忽略<return>節點級別,並且您有一個默認名稱空間,因爲您有沒有任何名稱空間的子節點,所以這是不正確的。因此,你需要:

SELECT Return.msgType 
    INTO lv_msgType 
    FROM XMLTABLE (
      xmlnamespaces (
       'http://schemas.xmlsoap.org/soap/envelope/' AS "soapenv", 
       'http://schemas.xmlsoap.org/soap/envelope/' AS "SOAP", 
       'urn:enterprise.com/ws/SAP/Finantial/MaintainMandate/V1' AS "ns1"), 
      '/soapenv:Envelope/SOAP:Body/ns1:maintainMandateResponse' 
      PASSING px_return 
      COLUMNS msgType VARCHAR2 (1) PATH 'return/messageType') Return; 

它得到:

PL/SQL procedure successfully completed. 

Message type: E 

或者你也可以將返回到課程的XPath,在這裏有同樣的效果:

   '/soapenv:Envelope/SOAP:Body/ns1:maintainMandateResponse/return' 
      PASSING px_return 
      COLUMNS msgType VARCHAR2 (1) PATH 'messageType') Return; 
+0

感謝亞歷克斯。這顯然是一個小路徑問題。 – milheiros