1

我的问题与这个问题相同,我使用的是 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 上下文中使用它。

4

2 回答 2

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
于 2015-03-22T22:06:59.200 回答
0

这篇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 中给出以下输出:

在此处输入图像描述

于 2015-03-22T22:17:46.013 回答