使用 XMLTable n Oracle 解析 XML

Parsing XML with XMLTable n Oracle

我有一个 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;
/**

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

对此进行更改以包含默认命名空间:

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

是的,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