2016-01-21 105 views
0

我無法理解其他我看到的問題,因爲它們有點不同。XML Oracle:從多個重複子節點中提取特定屬性

我從Web服務vi UTL_HTTP獲取XML作爲響應。 XML具有重複的子節點,我只想提取1個特定值。

響應XML:

<Customer> 
    <Loyalty> 
     <Client> 
      <Identifications> 
       <Identification> 
        <Form>Form1</Form> 
        <value>1234</value> 
       </Identification> 
       <Identification> 
        <Form>Form2</Form> 
        <value>4442</value> 
       </Identification> 
       <Identification> 
        <Form>Form3</Form> 
        <value>9995</value> 
       </Identification> 
      </Identifications> 
     </Client> 
    </Loyalty> 
</Customer> 

我需要提取的節點<value>僅其中節點<Form> = 「Form3」。

所以,我的代碼中,我收到來自另一個功能

v_ds_xml_response XMLTYPE; 
-- Here would lie the rest of the code (omitted) preparing the XML and next calling the function with it: 

V_DS_XML_RESPONSE := FUNCTION_CALL_WEBSERVICE(
     P_URL => V_DS_URL, --endpoint 
     P_DS_XML => V_DS_XML, --the request XML 
     P_ERROR => P_ERROR); 

隨着該響應,我創建了一個循環來存儲值。我試過使用WHERE甚至創建一個類型(下面的V_IDENTIFICATION是類型),但它沒有返回任何東西(null)。

for r IN (
SELECT   
ExtractValue(Value(p),'/Customer/Loyalty/Client/Identifications/Identification/*[local-name()="Form"]/text()') as form, 
    ExtractValue(Value(p),'/Customer/Loyalty/Client/Identifications/Identification/*[local-name()="value"]/text()') as value 
    FROM TABLE(XMLSequence(Extract(V_DS_XML_RESPONSE,'//*[local-name()="Customer"]'))) p 

LOOP 
V_IDENTIFICATION.FORM := r.form; 
V_IDENTIFICATION.VALUE := r.value; 
END LOOP; 

SELECT 
     V_IDENTIFICATION.VALUE 
INTO 
     P_LOYALTY_VALUE 
FROM dual 
WHERE V_IDENTIFICATION.TIPO = 'Form3'; 

注意,P_LOYALTY_VALUE是從我的程序

回答

0

AAAND我設法找到了解決方案,這是很簡單的,只是添加了[文本()= 「Form3」] /.../」到謂詞中的XPath如

SELECT   
ExtractValue(Value(p),'/Customer/Loyalty/Client/Identifications/Identification/*[local-name()="Form"][text()="Form3"]/text()') as form, 
ExtractValue(Value(p),'/Customer/Loyalty/Client/Identifications/Identification/Form[text()="Form3"]/.../*[local-name()="value"]/text()') as value 

還提取只需將它們直接發送到過程的OUT參數中即可:

P_FORM := r.form; 
P_LOYALTY_VALUE := r.value; 
2

的OUT參數與此SQL你應該得到所需的值:

with data as 
(select '<Customer> 
    <Loyalty> 
    <Client> 
     <Identifications> 
      <Identification> 
       <Form>Form1</Form> 
       <value>1234</value> 
      </Identification> 
      <Identification> 
       <Form>Form2</Form> 
       <value>4442</value> 
      </Identification> 
      <Identification> 
       <Form>Form3</Form> 
       <value>9995</value> 
      </Identification> 
     </Identifications> 
    </Client> 
</Loyalty> 
</Customer>' as xmlval 
    from dual b) 
(SELECT t.val 
    FROM data d, 
     xmltable('/Customer/Loyalty/Client/Identifications/Identification' 
        PASSING xmltype(d.xmlval) COLUMNS 
        form VARCHAR2(254) PATH './Form', 
        val VARCHAR2(254) PATH './value') t 
    where t.form = 'Form3'); 
1

當您不知道確切的路徑時。

select * from 
xmltable('for $i in $doc//*/Form 
where $i = "Form2" 
return $i/../value' 
passing 
xmltype('<Customer> 
    <Loyalty> 
     <Client> 
      <Identifications> 
       <Identification> 
        <Form>Form1</Form> 
        <value>1234</value> 
       </Identification> 
       <Identification> 
        <Form>Form2</Form> 
        <value>4442</value> 
       </Identification> 
       <Identification> 
        <Form>Form3</Form> 
        <value>9995</value> 
       </Identification> 
      </Identifications> 
     </Client> 
    </Loyalty> 
</Customer>') as "doc") 
0

要提取您尋找您可以使用下面的XPath表達式的值:

/Customer/Loyalty/Client/Identifications/Identification/Form[text()='Form3']/../value 

Test it here

然後你可以使用XMLTABLE函數來得到結果

SELECT my_value 
FROM xmltable(
     '/Customer/Loyalty/Client/Identifications/Identification/Form[text()=''Form3'']/../value' 
     PASSING xmltype(
'<Customer> 
    <Loyalty> 
     <Client> 
     <Identifications> 
      <Identification> 
       <Form>Form1</Form> 
       <value>1234</value> 
      </Identification> 
      <Identification> 
       <Form>Form2</Form> 
       <value>4442</value> 
      </Identification> 
      <Identification> 
       <Form>Form3</Form> 
       <value>9995</value> 
      </Identification> 
     </Identifications> 
     </Client> 
    </Loyalty> 
</Customer>') 
     COLUMNS 
      my_value VARCHAR2(4000) path '/value' 
    )