从XMLType Oracle中检索XML元素

Sta*_*ous 1 sql oracle xmltype oracle11g oracle11gr2

有人可以帮我从Oracle中的XMLType列中检索数据吗?

drop table xml_analysis;
create table xml_analysis(id number,soft_attributes XMLType);
create table xml_softattributes(id number,soft_attributes varchar2(200));
INSERT INTO xml_analysis VALUES 
   (       1, XMLType(
              '<softattributes> 
               <attr1>ABC</attr1>
               <attr2>XYZ</attr2> 
               <attr3>PQR</attr3> 
               </softattributes>
                '));
   insert into xml_softattributes values(1,'attr1');
   insert into xml_softattributes values(1,'attr2');
   insert into xml_softattributes values(1,'attr3');
Run Code Online (Sandbox Code Playgroud)
  1. 表xml_analysis包含xmltype列,其属性我不知道
  2. 表xml_softattributes包含softattributes列表(不是xpath),它们存在于xml_analysis表的xmltype列中
  3. 表是基于id连接的

现在我的问题是使用表xml_softattributes动态地从xml_analysis表中检索数据,我该怎么做?

需要输出

Softattribute Value 
=======================  
   attr1        ABC
   attr2        XYZ
   attr3        PQR
Run Code Online (Sandbox Code Playgroud)

我能想到的可能解决方案是使用动态字符串并执行,但我不希望动态字符串查询来检索数据.

Kau*_*yak 5

您可以使用如下组合existsNodeextract功能.

SELECT b.SOFT_ATTRIBUTES,
  CASE
    WHEN existsNode (a.soft_attributes ,'/*/'
      ||b.SOFT_ATTRIBUTES) = 1
    THEN a.soft_attributes.extract('/*/'
      ||b.SOFT_ATTRIBUTES
      ||'/text()').getStringVal()
  END value
FROM xml_analysis a,
  xml_softattributes b
WHERE a.id = b.id;
Run Code Online (Sandbox Code Playgroud)

* 用作匹配任何子节点的通配符.例如,/ PO/*/STREET匹配作为PO元素的孙子的任何街道元素.

输出:

attr1   ABC
attr2   XYZ
attr3   PQR
Run Code Online (Sandbox Code Playgroud)