如何从具有XML数据的表中获取特定数据

时间:2016-06-24 13:09:23

标签: sql xml oracle

这是我用来从XML获取数据的查询,该数据存储在表x的列中。

SELECT EXTRACTVALUE (IT.ADO_DATA, '/valueXML/IndividualADO/ado-name') "REFERENCE"
  FROM ITEM_ADO IT

这是XML数据

<valueXML xmlns="com.ado" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <IndividualADO>
    <ado-name>LIMS</ado-name>
    <field>
      <ado-field-name>ADO_LIMS_LIMS_Custodian_First_Name</ado-field-name>
      <ado-field-value xsi:nil="true"/>
    </field>
  </IndividualADO>
</valueXML>

我试图获取节点&#34; ado-name&#34;

的数据

1 个答案:

答案 0 :(得分:0)

假设ADO_DATA的类型为XMLType,则您的当前查询将不会返回任何值,因为您的valueXML节点具有命名空间,您需要将其包含在XPath中,或使用通配符:

SELECT EXTRACTVALUE (IT.ADO_DATA, '/*:valueXML/*:IndividualADO/*:ado-name') "REFERENCE"
  FROM ITEM_ADO IT;

REFERENCE                                                                      
--------------------------------------------------------------------------------
LIMS                                                                            

extractvalue()被遗弃了;您可以使用an XQuery执行相同的操作:

select xmlquery(
  '/*:valueXML/*:IndividualADO/*:ado-name/text()'
  passing ado_data
  returning content) as reference
from item_ado;

REFERENCE                                                                      
--------------------------------------------------------------------------------
LIMS

或使用默认命名空间而不是通配符(并显示用于测试的CTE):

with item_ado (ado_data) as (
  select xmltype('<valueXML xmlns="com.ado" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <IndividualADO>
    <ado-name>LIMS</ado-name>
    <field>
      <ado-field-name>ADO_LIMS_LIMS_Custodian_First_Name</ado-field-name>
      <ado-field-value xsi:nil="true"/>
    </field>
  </IndividualADO>
</valueXML>') from dual
)
select xmlquery(
  'declare default element namespace "com.ado"; (: :)
    /valueXML/IndividualADO/ado-name/text()'
  passing ado_data
  returning content) as reference
from item_ado;

REFERENCE                                                                      
--------------------------------------------------------------------------------
LIMS