简体   繁体   中英

Oracle SQL Query to retrieve specific Event using specific condition in xpath?

<List>
   <Event>
      <eventTime>2016-08-03T15:41:12.000+05:30</eventTime>
      <e:PlaceHolder xmlns:e="http://dpmt.element.com">US</e:PlaceHolder>
      <e:flag xmlns:e="http://dpmt.rmg.org/pq/rs">true</e:flag>
      <e:flag1 xmlns:e="http://dpmt.rmg.org/pq/rs">false</e:flag1>
   </Event>
   <Event>
      <eventTime>2016-08-01T19:41:12.000+05:30</eventTime>
   </Event>
</List>

I have stored my XML document in such a way , which may contains multiple <Event> tags, using the X-Path query , I want to fetch only that event , that have e:flag value true, In my case i have to get only first event tag using where clause. I need a generic query using EXTRACT or ExtractValue function by condition in where clause to check its flag value.

I am writing query in this way :-

SELECT * FROM (SELECT EXTRACT(doc,'//List').getClobVal() AS doc FROM my_table) T;

This is gonna be help you; Flag tag is tested with null value. Another values should be added in function.

 DECLARE
vs_Xml VARCHAR2(32000):= '<List>
  <Event>
    <eventTime>2016-08-03T15:41:12.000+05:30</eventTime>
    <e:PlaceHolder xmlns:e="http://dpmt.element.com">US</e:PlaceHolder>
    <e:flag xmlns:e="http://dpmt.rmg.org/pq/rs">true</e:flag>
    <e:flag1 xmlns:e="http://dpmt.rmg.org/pq/rs">false</e:flag1>
</Event>
   <Event>
     <eventTime>2016-08-01T19:41:12.000+05:30</eventTime>
   </Event>
</List>';

vx_ParameterList   XMLTYPE;
vx_Parameter       XMLTYPE;
vn_ParameterIndex  NUMBER;
vs_Key             VARCHAR2(64);
vs_XPath           VARCHAR2(255);
vs_Value           VARCHAR2(10000);
vs_Value2           VARCHAR2(10000);

BEGIN
   vx_ParameterList := xmltype(vs_Xml);
   vn_ParameterIndex := 1;
   vs_XPath := '/List/Event'; 

WHILE vx_ParameterList.existsNode(vs_XPath || '[' || vn_ParameterIndex || ']') = 1 LOOP
  vx_Parameter := vx_ParameterList.extract(vs_XPath || '[' || vn_ParameterIndex || ']');

  vs_Value := vx_Parameter.extract('//eventTime/text()').GetStringVal(); 

  SELECT  vx_Parameter.extract('//flag/text()', 'xmlns:e="http://dpmt.rmg.org/pq/rs"').GetStringVal() INTO  vs_Value2  FROM DUAL; --Null value handling
  vn_ParameterIndex := vn_ParameterIndex + 1;

  dbms_output.put_line(vs_Value ||'-' || vs_Value2);
END LOOP;

 END;
/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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