简体   繁体   English

Oracle 11g的XMLtable

[英]XMLtable with Oracle 11g

Here is a sample table: 这是一个示例表:

create table xmltemp (mydoc xmltype)

Here is a small xml doc for it: 这是一个小的xml文档:

insert into xmltemp values (
xmltype
('<?xml version="1.0"?>
<countries>
  <country>
    <name>Canada</name>
  </country>
  <country>
    <name>US</name>
    <states>
      <state>
        <name>Washington</name>
        <name>Oregon</name>        
      </state>
    </states>
  </country>
</countries>
')
)  

Notice that Canada does not have a 'states' element but the US does. 请注意,加拿大没有“州”元素,但美国却有。 I'm trying to get these query results (order and formatting is not important): 我正在尝试获取这些查询结果(顺序和格式不重要):

Canada,
US,Washington
US,Oregon

When I execute this, I see both Canada and the US in the result: 当我执行此操作时,我在结果中看到加拿大和美国:

select
countryname
from xmltemp,
xmltable('/countries/country' passing mydoc
   columns countryname varchar2(10) path 'name') 

When I do this, I get both the states: 当我这样做时,我得到两个状态:

select
statename
from xmltemp,
xmltable('/countries/country/states/state/name' passing mydoc
   columns statename   varchar2(20) path '.') c

I tried this to get both country and states, but it seems oracle does not like the '..' syntax: 我试过这个以获得国家和州,但似乎oracle不喜欢'..'语法:

select
statename
from xmltemp,
xmltable('/countries/country/states/state/name' passing mydoc
   columns statename   varchar2(20) path '.',
           countryname varchar2(20) path '../../../name') c

Heres the error: 继承人错误:

ORA-19110: unsupported XQuery expression

When I try this, I get the 'multi-item' error because of the two states: 当我尝试这个时,由于以下两种状态,我得到“多项目”错误:

select
countryname,
statename
from xmltemp,
xmltable('/countries/country' passing mydoc
   columns countryname varchar2(10) path 'name',
           statename   varchar2(20) path 'states/state/name') c

Here is that error: 这是错误:

ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton 
sequence - got multi-item sequence

What's a query that will get me my desired output of: 什么是一个查询,它将获得我想要的输出:

Canada,
US,Washington
US,Oregon

Thanks 谢谢

Try this: 尝试这个:

select      X.COUNTRYNAME, Y.STATENAME
from        XMLTEMP
           ,xmltable('/countries/country'
                     passing MYDOC
                     columns COUNTRYNAME varchar2(20) path './name', 
                             STATES xmltype path './states') X,
            xmltable('/states/state/name' passing X.STATES 
                    columns STATENAME varchar2(20) path '.') (+) Y

Because you have multiple states you should join to another xml table. 因为您有多个状态,所以应该加入另一个xml表。 As some countries have no states then it needs to be a left outer join. 由于一些国家没有州,那么它需要是一个左外连接。 I'm using the old method of (+) as I'm trying this on 10g and it seems there's a problem using left outer join in 10g but apparently it should be fine in 11g . 我正在使用(+)的旧方法,因为我在10g上尝试这个并且似乎在10g中使用left outer join存在问题,但显然它在11g应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM