使用XQUERY节点函数将值提取到行中

时间:2015-03-22 21:41:39

标签: oracle xquery-sql oracle-xml-db

我的问题与此one相同,但我使用的是Oracle 12.1而不是SQL Server。

我想从以下xml文档中提取所有名称元素:

<ci5>
   <employee>
      <name>George Cowley</name>  
      <salary via="B">80000</salary>
   </employee>
   <employee>
      <name>William Bodie</name>  
      <salary via="C">40000</salary>
   </employee>
   <employee>
      <name>Ray Doyle</name>  
      <salary via="C">40000</salary>
   </employee>
</ci5>  

我想要的输出是三个独立的行,例如:

<name>George Cowley</name>
<name>William Bodie</name>
<name>Ray Doyle</name>

使用以下查询

SELECT XMLQUERY('ci5/employee/name' 
                  PASSING BY VALUE XMLTYPE.CREATEXML('<ci5><employee><name>George Cowley</name><salary via="B">80000</salary></employee><employee><name>William Bodie</name><salary via="C">40000</salary></employee><employee><name>Ray Doyle</name><salary via="C">40000</salary></employee></ci5>') RETURNING CONTENT) x
   FROM dual;  

 SELECT XMLQUERY('for $i in /ci5
                 return $i/employee/name'
                 PASSING BY VALUE XMLTYPE.CREATEXML('<ci5><employee><name>George Cowley</name><salary via="B">80000</salary></employee><employee><name>William Bodie</name><salary via="C">40000</salary></employee><employee><name>Ray Doyle</name><salary via="C">40000</salary></employee></ci5>') RETURNING CONTENT) x
   FROM dual;  

显示名称,但输出全部在一行:

<name>George Cowley</name><name>William Bodie</name><name>Ray Doyle</name>

看起来我需要使用NODE函数,但我不知道如何在Oracle上下文中使用它。

2 个答案:

答案 0 :(得分:1)

我认为你不需要任何xquery。一个简单的xpath表达式就足够了。这有两个选择。第一个返回XML序列(单独的xml节点),第二个返回表行:

    WITH test AS
      (SELECT xmltype('<ci5>   
    <employee>      
    <name>George Cowley</name>        
    <salary via="B">80000</salary>   
    </employee>   
    <employee>      
    <name>William Bodie</name>        
    <salary via="C">40000</salary>   
    </employee>   
    <employee>      
    <name>Ray Doyle</name>        
    <salary via="C">40000</salary>   
    </employee>
    </ci5>  ') data
      FROM dual
      )
    SELECT xmlsequence(extract(data,'/ci5/employee/name')) FROM test

WITH test AS
      (SELECT xmltype('<ci5>   
    <employee>      
    <name>George Cowley</name>        
    <salary via="B">80000</salary>   
    </employee>   
    <employee>      
    <name>William Bodie</name>        
    <salary via="C">40000</salary>   
    </employee>   
    <employee>      
    <name>Ray Doyle</name>        
    <salary via="C">40000</salary>   
    </employee>
    </ci5>  ') data
      FROM dual
      )
    SELECT extract(value(d), '*') AS name
    FROM test,
      TABLE(xmlsequence(extract(data, '/ci5/employee/name'))) d

答案 1 :(得分:0)

This Oracle白皮书回答了这个问题,我需要使用XMLTABLE

  

...将XQuery评估的结果映射到关系行和   列...

更改我的查询以使用XMLTABLE

 SELECT x.ename
   FROM XMLTABLE('/ci5/employee/name'                 
                 PASSING XMLTYPE.CREATEXML('<ci5><employee><name>George Cowley</name><salary via="B">80000</salary></employee><employee><name>William Bodie</name><salary via="C">40000</salary></employee><employee><name>Ray Doyle</name><salary via="C">40000</salary></employee></ci5>') 
                 COLUMNS ename VARCHAR2(30) PATH '/name') x 

在SQL Developer中提供以下输出:

enter image description here