使用 XMLTable 函数从 Xml 中提取数据

Extracting Data from Xml Using XMLTable Function

我刚开始学习在 PLSQL 中处理 XML 数据,这是我的问题。

创建此问题中使用的表所需的代码。

 create table purchase_order 
(
data XMLType
);

insert into purchase_order
values(XMLType('<PurchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation=
    "http://localhost:8080/source/schemas/poSource/xsd/purchaseOrder.xsd">
  <Reference>SBELL-2002100912333601PDT</Reference>
  <Actions>
    <Action>
      <User>SVOLLMAN</User>
    </Action>
  </Actions>
  <Reject/>
  <Requestor>Sarah J. Bell</Requestor>
  <User>SBELL</User>
  <CostCenter>S30</CostCenter>
  <ShippingInstructions>
    <name>Sarah J. Bell</name>
    <address>400 Oracle Parkway
      Redwood Shores
      CA
      94065
      USA</address>
    <telephone>650 506 7400</telephone>
  </ShippingInstructions>
  <SpecialInstructions>Air Mail</SpecialInstructions>
  <LineItems>
    <LineItem ItemNumber="1">
      <Description>A Night to Remember</Description>
      <Part Id="715515009058" UnitPrice="39.95" Quantity="2"/>
    </LineItem>
    <LineItem ItemNumber="2">
      <Description>The Unbearable Lightness Of Being</Description>
      <Part Id="37429140222" UnitPrice="29.95" Quantity="2"/>
    </LineItem>
    <LineItem ItemNumber="3">
      <Description>Sisters</Description>
      <Part Id="715515011020" UnitPrice="29.95" Quantity="4"/>
    </LineItem>
  </LineItems>
</PurchaseOrder>'));

我想提取 ItemNumber、Description、Part Id、UnitPrice、Quantity 等值,并将它们显示为关系 Table。但我收到错误 ORA-19279。这是我的代码。

select x.*
from purchase_order t,
xmltable('/PurchaseOrder'
passing t.data 
columns Reference varchar2(300) path 'Reference',
Usr varchar2(20) path '//Action',
Requestor varchar2(20) path '//Requestor',
CostCenter varchar2(20) path '//CostCenter',
ShippingInstructions varchar2(500) path '//ShippingInstructions',
SpecialInstructions varchar2(50) path '//SpecialInstructions',
ItemNumber varchar(10) path '//LineItems/LineItem/@ItemNumber',
Description varchar(100) path '//Description'

 ) x

您需要将多项目 XML 元素从一个级别传递到第二个 XMLTable() 调用:

select x.Usr, -- other x columns, but not LineItems
  y.ItemNumber, y.Description
from purchase_order t,
  xmltable('/PurchaseOrder'
    passing t.data 
    columns Reference varchar2(300) path 'Reference',
    Usr varchar2(20) path '//Action',
    Requestor varchar2(20) path '//Requestor',
    CostCenter varchar2(20) path '//CostCenter',
    ShippingInstructions varchar2(500) path '//ShippingInstructions',
    SpecialInstructions varchar2(50) path '//SpecialInstructions',
    LineItems XMLType path '//LineItems'
  ) x,
  xmltable('/LineItems/LineItem'
    passing x.LineItems
    columns ItemNumber varchar(10) path '//LineItem/@ItemNumber',
    Description varchar(100) path '//Description'
  ) y;

USR                  ITEMNUMBER DESCRIPTION                             
-------------------- ---------- ----------------------------------------
SVOLLMAN             1          A Night to Remember                      
SVOLLMAN             2          The Unbearable Lightness Of Being        
SVOLLMAN             3          Sisters                                  

我只显示了 x 中的一列以防止滚动,但您可以包括所有这些列,但传递给第二个 XMLTable() 调用的 LineItems 列除外;所以你不能使用 select *.