Oracle

XML 列中的 Where 子句

  • February 4, 2019

我的 Oracle 11 G DB 中有以下 XML 列:

<?xml version="1.0" encoding="UTF-8"?>
<l-analysis version="2.9">
  <vehicle>
     <odo-reading latest="true">
        <reading value="653463">653,463</reading>
        <reading-date value="2018-12-04">4-Dec-2018</reading-date>
        <reading-unit code="K">Km</reading-unit>
     </odo-reading>
 <odo-reading latest="false">
    <reading value="391264">393,264</reading>
    <reading-date value="2018-12-04">4-Dec-2018</reading-date>
    <reading-unit code="K">Km</reading-unit>
 </odo-reading>
  </vehicle>
</l-analysis>

我需要查詢reading value但只有where odo-reading latest="true".

我怎樣才能在CLOB專欄中做到這一點?

drop table t1 purge;
create table t1 (id number, cxml clob);
insert into t1 values (1,
'<?xml version="1.0" encoding="UTF-8"?>
<l-analysis version="2.9">
  <vehicle>
     <odo-reading latest="true">
        <reading value="653463">653,463</reading>
        <reading-date value="2018-12-04">4-Dec-2018</reading-date>
        <reading-unit code="K">Km</reading-unit>
     </odo-reading>
 <odo-reading latest="false">
    <reading value="391264">393,264</reading>
    <reading-date value="2018-12-04">4-Dec-2018</reading-date>
    <reading-unit code="K">Km</reading-unit>
 </odo-reading>
  </vehicle>
</l-analysis>');
commit;

select extractvalue(xmltype(cxml), 'l-analysis/vehicle/odo-reading[@latest="true"]/reading/@value') as value from t1;

VALUE     
----------
653463

在編輯之前回答原始問題:

drop table t1 purge;
create table t1 (id number, cxml clob);
insert into t1 values (1,
'<?xml version="1.0" encoding="UTF-8"?>
<l-analysis version="2.9">
  <vehicle>
     <odo-reading latest="true">
        <reading value="653463">653,463</reading>
        <reading-date value="2018-12-04">4-Dec-2018</reading-date>
        <reading-unit code="K">Km</reading-unit>
     </odo-reading>
  </vehicle>
</l-analysis>');
insert into t1 values (2,
'<?xml version="1.0" encoding="UTF-8"?>
<l-analysis version="2.9">
  <vehicle>
     <odo-reading latest="false">
        <reading value="653463">653,464</reading>
        <reading-date value="2018-12-04">4-Dec-2018</reading-date>
        <reading-unit code="K">Km</reading-unit>
     </odo-reading>
  </vehicle>
</l-analysis>');
commit;

select id, extractvalue(xmltype(cxml), 'l-analysis/vehicle/odo-reading/reading/@value') as value from t1
where extractvalue(xmltype(cxml), 'l-analysis/vehicle/odo-reading/@latest') = 'true';   

       ID VALUE     
---------- ----------
        1 653463   

引用自:https://dba.stackexchange.com/questions/228889