如何从oracle 10g中的XML类型列中选择值

时间:2015-09-29 07:18:34

标签: sql xml oracle oracle10g

下面是我的xml列,名称为xml_col。我想选择ProductType为productname且ProductCode为1111的所有计划名称。

<life>
 <Holding id="Holding">
<HoldingTypeCode tc="2">Policy</HoldingTypeCode>
<Purpose tc="11">example</Purpose>
<Policy>
  <ProductType tc="235684546454">productname</ProductType>
  <ProductCode>1111</ProductCode>
  <PlanName>name_plan</PlanName>
   </Policy>
 </Holding>
</life>

我尝试了以下简单的选择,它不适合我。请帮忙

select extractValue(xml_col,'/LifE/Policy/PlanName') "plan" 
FROM mytable

查询mt场景,我查询如下,显示错误“ORA-00904:”schemaname“。”tablename“。”xml_col“:无效标识符”请帮忙

 select extractValue(xml_col,'/lifE/Holding/Policy/PlanName') "plan name" FROM schemaname.tablename WHERE schemaname.tablename.xml_col.exist('/lifE/Holding/Policy/ProductCode[text()  = "1111"]') = 1  

2 个答案:

答案 0 :(得分:1)

@mikron是对的。 XML区分大小写。

select extractValue(xml_col,'/life/Holding/Policy/PlanName') "plan" 
FROM mytable

编辑:根据@mikron

的指示更新

答案 1 :(得分:1)

如果我是你,我不会使用extract或extractvalue,因为自Oracle 10g以来它们都被弃用了。相反,使用XMLTABLE,如:

with sample_data as (select xmltype(
'<life>
  <Holding id="Holding">
    <HoldingTypeCode tc="2">Policy</HoldingTypeCode>
    <Purpose tc="11">example</Purpose>
    <Policy>
      <ProductType tc="235684546454">productname</ProductType>
      <ProductCode>1111</ProductCode>
      <PlanName>name_plan</PlanName>
    </Policy>
  </Holding>
  <Holding id="Holding">
    <HoldingTypeCode tc="2">Policy</HoldingTypeCode>
    <Purpose tc="11">example</Purpose>
    <Policy>
      <ProductType tc="235684546454">productname</ProductType>
      <ProductCode>1112</ProductCode>
      <PlanName>name_plan</PlanName>
    </Policy>
  </Holding>
</life>') xml_data from dual)
select xml_data,
       x.*
from   sample_data sd
       cross join xmltable('/life/Holding'
                           passing sd.xml_data
                           columns product_type varchar2(20) path 'Policy/ProductType',
                                   product_code number path 'Policy/ProductCode',
                                   plan_name varchar2(20) path 'Policy/PlanName') x
where  x.product_type = 'productname'
and    x.product_code = 1111;

XML_DATA   PRODUCT_TYPE PRODUCT_CODE PLAN_NAME 
---------- ------------ ------------ ---------
<life>...  productname          1111 name_plan