【问题标题】:Parsing XML with XMLTable n Oracle使用 XMLTable n Oracle 解析 XML
【发布时间】:2020-04-29 13:32:30
【问题描述】:

我有一个包含客户和订单详细信息的 xml。我正在使用 XMLTable 解析它。我想我给出了正确的 XPath,但我没有得到任何输出。

这是我尝试过的。希望得到帮助。

**DECLARE
l_raw_xml CLOB:= '<?xml version="1.0" encoding="utf-8"?>
<Root
    xmlns="http://www.adventure-works.com">
    <Customers>
        <Customer CustomerID="GREAL">
            <CompanyName>Great Lakes Food Market</CompanyName>
            <ContactName>Howard Snyder</ContactName>
            <ContactTitle>Marketing Manager</ContactTitle>
            <Phone>(503) 555-7555</Phone>
            <FullAddress>
                <Address>2732 Baker Blvd.</Address>
                <City>Eugene</City>
                <Region>OR</Region>
                <PostalCode>97403</PostalCode>
                <Country>USA</Country>
            </FullAddress>
        </Customer>
    </Customers>
    <Orders>
        <Order>
            <CustomerID>LETSS</CustomerID>
            <EmployeeID>6</EmployeeID>
            <OrderDate>1997-11-10T00:00:00</OrderDate>
            <RequiredDate>1997-12-08T00:00:00</RequiredDate>
            <ShipInfo ShippedDate="1997-11-21T00:00:00">
                <ShipVia>2</ShipVia>
                <Freight>45.97</Freight>
                <ShipName>Let Stop N Shop</ShipName>
                <ShipAddress>87 Polk St. Suite 5</ShipAddress>
                <ShipCity>San Francisco</ShipCity>
                <ShipRegion>CA</ShipRegion>
                <ShipPostalCode>94117</ShipPostalCode>
                <ShipCountry>USA</ShipCountry>
            </ShipInfo>
        </Order>
    </Orders>
</Root>';
l_xml_type XMLTYPE:=XMLTYPE.createXML(l_raw_xml);
cursor c_make_xml_object 
is
    SELECT x.*,
           y.*
      FROM XMLTABLE('/Root/Customers/Customer'
                    PASSING l_xml_type
                    COLUMNS
                        CustomerID  VARCHAR2(255) PATH '@CustomerID',
                        CompanyName VARCHAR2(255) PATH 'CompanyName',
                        FullAddress  XMLTYPE PATH 'FullAddress') x,
           XMLTABLE('/FullAddress'
                    PASSING x.FullAddress
                        COLUMNS
                            Address VARCHAR2(255) PATH 'Address',
                            City    VARCHAR2(255) PATH 'City') y ;

BEGIN
    FOR rec IN c_make_xml_object 
    loop
        DBMS_OUTPUT.PUT_LINE(rec.CustomerID);
        DBMS_OUTPUT.PUT_LINE(rec.CompanyName);
    END LOOP;
END;
/**

我必须考虑命名空间吗?我添加了命名空间代码,但它不起作用/

【问题讨论】:

    标签: sql xml oracle plsql xml-parsing


    【解决方案1】:

    是的,在 SQL 查询中需要正确的命名空间定义,如下所示:

    SELECT x.*, y.*
      FROM XML_TAB t,
           XMLTABLE(XMLnamespaces('http://www.adventure-works.com' as "ns"),
                    'ns:Root/ns:Customers/ns:Customer'
                    PASSING t.xml_data
                    COLUMNS
                        CustomerID  VARCHAR2(255) PATH '@CustomerID',
                        CompanyName VARCHAR2(255) PATH 'ns:CompanyName',
                        FullAddress XMLTYPE       PATH 'ns:FullAddress'
                        ) x,
           XMLTABLE(XMLnamespaces('http://www.adventure-works.com' as "ns"),
                    'ns:FullAddress'
                    PASSING x.FullAddress
                    COLUMNS
                        Address VARCHAR2(255) PATH 'ns:Address',
                        City    VARCHAR2(255) PATH 'ns:City') y ;   
    

    Demo with SQL

    Demo with PL/SQL

    【讨论】:

      【解决方案2】:

      更改为包含默认命名空间:

        FROM XMLTABLE(xmlnamespaces(default 'http://www.adventure-works.com'), 
                '/Root/Customers/Customer'  ....
          ....                   
              XMLTABLE(xmlnamespaces(default 'http://www.adventure-works.com'), '/FullAddress'
                         .....
      

      【讨论】:

        猜你喜欢
        • 2014-08-14
        • 2020-03-10
        • 2016-04-28
        • 2014-04-08
        • 2017-06-01
        • 2020-03-11
        • 1970-01-01
        • 2017-12-06
        • 1970-01-01
        相关资源
        最近更新 更多