从oracle数据库中的XMLtype列查询值

时间:2015-03-04 12:14:42

标签: xml oracle oracle11g

我有一个XMLtype列,其中包含XML_TABLE (ID NUMBER, XML_DATA XMlTYPE)这样的表格。 然后我插入一个像

的值
INSERT INTO xml_table (1, XMLtype('<current>
  <city id="2643743" name="London">
    <coord lon="-0.13" lat="51.51"/>
    <country>GB</country>
    <sun rise="2015-03-04T06:38:20" set="2015-03-04T17:46:01"/>
  </city>
  <temperature value="280.71" min="280.15" max="281.15" unit="kelvin"/>
  <humidity value="77" unit="%"/>
  <pressure value="1029" unit="hPa"/>
</current>'));

现在我想查询这个表。我可以轻松选择国家&#39;通过以下查询

select t.xml_data.extract('/current/city/country/text()').getStringVal() "XML Data"
from xml_table t;

但我无法通过此查询选择温度值。现在如何从表格中选择温度值?

1 个答案:

答案 0 :(得分:4)

EXTRACT(和EXTRACTVALUE)在10g及以上版本中已被弃用。您应该使用XMLTABLE代替:

with xml_table as (select 1 id, XMLtype('<current>
  <city id="2643743" name="London">
    <coord lon="-0.13" lat="51.51"/>
    <country>GB</country>
    <sun rise="2015-03-04T06:38:20" set="2015-03-04T17:46:01"/>
  </city>
  <temperature value="280.71" min="280.15" max="281.15" unit="kelvin"/>
  <humidity value="77" unit="%"/>
  <pressure value="1029" unit="hPa"/>
</current>') xml_data from dual)
select x.*
from   xml_table xt,
       xmltable('/current' passing xt.xml_data
                columns country varchar2(10) path 'city/country',
                        temp_k number path 'temperature/@value') x;

COUNTRY        TEMP_K
---------- ----------
GB             280.71