Oracle DB - 使用XMLTABLE提取XML数据

时间:2014-11-12 13:30:17

标签: oracle xml-parsing xquery xmltable

我在Oracle 10g数据库中使用XMLTABLE函数,并希望将XML中的值提取到Oracle Table中。

 SELECT * 
 FROM xmltable(
                xmlnamespaces ('http://www.cool.com/totem/1.1' AS  "n1"), '/n1:totem/n1:results' 
                PASSING xmltype.createxml(('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                                            <totem xmlns="http://www.cool.com/totem/1.1">
                                                <results>
                                                    <valuationDate>2014-07-31</valuationDate>
                                                    <clientID>220</clientID>
                                                    <energy>
                                                        <underlier>
                                                            <name>CO2 CER</name>
                                                            <group>European Emissions</group>
                                                            <units>EUR / MT</units>
                                                            <pricingTime>LDN 17:00</pricingTime>
                                                            <instrument>
                                                                <period>Month</period>
                                                                <startDate>2014-12-01</startDate>
                                                                <endDate>2014-12-31</endDate>
                                                                <type>Forward</type>
                                                                <price>0.25852</price>
                                                                <priceOut>r</priceOut>
                                                                <contributors>15</contributors>
                                                            </instrument>
                                                        </underlier>
                                                                                                                <underlier>
                                                            <name>CO2 CER</name>
                                                            <group>European Emissions</group>
                                                            <units>EUR / MT</units>
                                                            <pricingTime>LDN 17:00</pricingTime>
                                                            <instrument>
                                                                <period>Month</period>
                                                                <startDate>2014-12-01</startDate>
                                                                <endDate>2014-12-31</endDate>
                                                                <type>Forward</type>
                                                                <price>0.25852</price>
                                                                <priceOut>r</priceOut>
                                                                <contributors>15</contributors>
                                                            </instrument>
                                                        </underlier>
                                                    </energy>
                                                </results>
                                            </totem>'
                                            ))
                COLUMNS     valuationDate        varchar2(500)          PATH 'n1:valuationDate', 
                                   clientID             varchar2(500)          PATH 'n1:clientID', 
                                   name                 varchar2(500)          PATH 'n1:energy/n1:underlier/n1:name',
                                   group1               varchar2(500)        PATH 'n1:energy/n1:underlier/n1:group',
                                   units                varchar2(500)          PATH 'n1:energy/n1:underlier/n1:units',
                                   pricingTime          varchar2(500)          PATH 'n1:energy/n1:underlier/n1:pricingTime',   
                                   period               varchar2(500)          PATH 'n1:energy/n1:underlier/n1:instrument/n1:period',  
                                   startDate            varchar2(500)          PATH 'n1:energy/n1:underlier/n1:instrument/n1:startDate',  
                                   endDate              varchar2(500)          PATH 'n1:energy/n1:underlier/n1:instrument/n1:endDate',
                                   type                 varchar2(500)          PATH 'n1:energy/n1:underlier/n1:instrument/n1:type',
                                   price                varchar2(500)          PATH 'n1:energy/n1:underlier/n1:instrument/n1:price',
                                   priceOut             varchar2(500)          PATH 'n1:energy/n1:underlier/n1:instrument/n1:priceOut',
                                   contributors         varchar2(500)          PATH 'n1:energy/n1:underlier/n1:instrument/n1:contributors'
   ) AS instrument 

o / p是:

XMLNLS_NS   VALUATIONDATE   CLIENTID    NAME    GROUP1  PERIOD  STARTDATE   ENDDATE TYPE    PRICE   PRICEOUT    CONTRIBUTORS
            2014-07-31      220     

如何提取/填充其余标记的值?

编辑:谢谢。 但是当我有一个以上的下划线时,我会得到关注 错误ORA-19279:XPTY0004 - XQuery动态类型不匹配:预期的单例序列 - 得到多项序列

1 个答案:

答案 0 :(得分:1)

使用正确的命名空间。命名空间是继承的,因此基本上所有元素都在http://www.cool.com/totem/1.1命名空间中。例如,

n1:results/energy/underlier/name

应该是

n1:results/n1:energy/n1:underlier/n1:name