2015-01-12 65 views
2

我剛開始學習處理PLSQL中的XML數據,這是我的問題。使用XMLTable函數從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之類的值並將它們顯示爲關係表。但是我得到錯誤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 
+0

@AlexPoole ORA-19279 XQuery動態類型不匹配。期望的單例序列-got多項目序列。 – redsoxlost

回答

1

你需要從一個層次多項目的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一列,以防止滾動,但你可以包括所有其中LineItems列除第二個XMLTable()調用;所以你不能使用select *