繁体   English   中英

XML Oracle:从多个重复的子节点中提取特定属性

[英]XML Oracle: Extract specific attribute from multiple repeating child nodes

我在理解其他问题时遇到了麻烦,因为它们有些不同。

我从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>

我仅在节点<Form> =“ Form3”的情况下才需要提取节点<value>

因此,在我的代码中,我收到了另一个函数的响应

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); 

这样,我创建了一个LOOP来存储值。 我尝试使用WHERE甚至创建类型(下面的V_IDENTIFICATION是该类型),但是它没有返回任何内容(空)。

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是我的过程中的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');

当您不知道确切路径时。

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" ) 

要提取您要查找的值,可以使用以下XPATH表达式:

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

在这里测试

然后可以使用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'
    )

我设法找到了一个非常简单的解决方案,只是添加了[text()=“ 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM