1

我正在使用 Oracle 11g XMLTABLE 函数从附加的 XML 文件中获取数据。当我执行代码时,我得到 3 行。我正在检索产品,为与客户对应的每个产品正确生成一行。问题是我没有得到其余的客户数据,我得到了 CustomerName、CustomerDOB 和 CustomerEmail 的空值。出于某种原因,我正在使用的代码,Oracle 没有获取 2 个级别。请帮忙!我究竟做错了什么?

XML

<?xml version="1.0" encoding="iso-8859-1"?>
<USACustomers>
    <NewYorkCustomers>
        <Customer>
            <CustomerData>
                <CustomerName>John Stark</CustomerName>
                <CustomerDOB>07/09/1973</CustomerDOB>
                <CustomerEmail>j.stark@liquid.com</CustomerEmail>
            </CustomerData>
            <CustomerAddress>
                <Address>400 Railroad Way</Address>
                <City>Larchmont</City>
                <State>NY</State>
                <Zip>10542</Zip>
            </CustomerAddress>
            <CustomerProducts>
                <CustomerProduct>
                    <PoductType>CTD</PoductType>
                    <ProductNumber>2050035302</ProductNumber>
                </CustomerProduct>
                <CustomerProduct>
                    <PoductType>REC</PoductType>
                    <ProductNumber>2050920400</ProductNumber>
                </CustomerProduct>
            </CustomerProducts>
        </Customer>
        <Customer>
            <CustomerData>
                <CustomerName>Maria Stark</CustomerName>
                <CustomerDOB>07/09/1972</CustomerDOB>
                <CustomerEmail>m.stark@liquid.com</CustomerEmail>
            </CustomerData>
            <CustomerAddress>
                <Address>410 Railroad Way</Address>
                <City>Larchmont</City>
                <State>NY</State>
                <Zip>10542</Zip>
            </CustomerAddress>
            <CustomerProducts>
                <CustomerProduct>
                    <PoductType>REC</PoductType>
                    <ProductNumber>2050920400</ProductNumber>
                </CustomerProduct>
            </CustomerProducts>
        </Customer>
    </NewYorkCustomers>
</USACustomers>

代码

SELECT                            
CustomerName,
CustomerDOB,
CustomerEmail,
PoductType


FROM   XMLTABLE (                    '/USACustomers/NewYorkCustomers/Customer/CustomerProducts/CustomerProduct'
PASSING xmltype (BFILENAME ('BACKUP', 'TEST.xml'), NLS_CHARSET_ID ('AL32UTF8'))

COLUMNS CustomerName  VARCHAR2(50)    PATH 'CustomerData/CustomerName',
        CustomerDOB   VARCHAR2(50)    PATH 'CustomerData/CustomerDOB',
        CustomerEmail VARCHAR2(50)    PATH 'CustomerData/CustomerEmail',
        PoductType    VARCHAR2(50)    PATH 'PoductType',
        ProductNumber VARCHAR2(50)    PATH 'ProductNumber');
4

2 回答 2

0

嵌套它,因为您想首先提取客户数据,然后迭代每个产品。

select customername, customerdob, customeremail, PoductType, ProductNumber
  from (select customername, customerdob, customeremail, products
           from xmltable('/USACustomers/NewYorkCustomers/Customer' 
                         PASSING xmltype (BFILENAME ('BACKUP', 'TEST.xml'), NLS_CHARSET_ID ('AL32UTF8'))
                         columns customername varchar2(50) path 'CustomerData/CustomerName',
                                 customerdob varchar2(50) path 'CustomerData/CustomerDOB',
                                 customeremail varchar2(50) path 'CustomerData/CustomerEmail',
                                 products xmltype path 'CustomerProducts')) cust,
       xmltable('/CustomerProducts/CustomerProduct'
                passing cust.products
                columns PoductType    VARCHAR2(50)    PATH 'PoductType',
                        ProductNumber VARCHAR2(50)    PATH 'ProductNumber');

例如:

SQL> select customername, customerdob, customeremail, PoductType, ProductNumber
  2    from (select customername, customerdob, customeremail, products
  3             from mytable l,
  4                  xmltable('/USACustomers/NewYorkCustomers/Customer'
  5                           passing l.x
  6                           columns customername varchar2(50) path 'CustomerData/CustomerName',
  7                                   customerdob varchar2(50) path 'CustomerData/CustomerDOB',
  8                                   customeremail varchar2(50) path 'CustomerData/CustomerEmail',
  9                                   products xmltype path 'CustomerProducts')) cust,
 10         xmltable('/CustomerProducts/CustomerProduct'
 11                  passing cust.products
 12                  columns PoductType    VARCHAR2(50)    PATH 'PoductType',
 13                          ProductNumber VARCHAR2(50)    PATH 'ProductNumber');

CUSTOMERNAME         CUSTOMERDO CUSTOMEREMAIL        PODUCTTYPE PRODUCTNUMBER
-------------------- ---------- -------------------- ---------- -------------
John Stark           07/09/1973 j.stark@liquid.com   CTD        2050035302
John Stark           07/09/1973 j.stark@liquid.com   REC        2050920400
Maria Stark          07/09/1972 m.stark@liquid.com   REC        2050920400
于 2013-01-15T16:06:44.523 回答
0

你可以这样做(一个xmltable()电话:

SELECT                            
CustomerName,
CustomerDOB,
CustomerEmail,
PoductType


FROM   XMLTABLE ('for $i in $custt/USACustomers/NewYorkCustomers/Customer, 
                 $j in $i//CustomerProduct 
                 return <deNormalizedCust>{$i//CustomerData}{$j}</deNormalizedCust>'
PASSING xmltype(BFILENAME ('BACKUP', 'TEST.xml'), NLS_CHARSET_ID ('AL32UTF8'))
as "custt"
COLUMNS CustomerName  VARCHAR2(50)    PATH '/deNormalizedCust/CustomerData/CustomerName',
        CustomerDOB   VARCHAR2(50)    PATH '/deNormalizedCust/CustomerData/CustomerDOB',
        CustomerEmail VARCHAR2(50)    PATH '/deNormalizedCust/CustomerData/CustomerEmail',
        PoductType    VARCHAR2(50)    PATH '/deNormalizedCust/CustomerProduct/PoductType',
        ProductNumber VARCHAR2(50)    PATH '/deNormalizedCust/CustomerProduct/ProductNumber');

这是一个 sqlfiddle 演示

于 2013-01-16T10:15:15.050 回答