2017-02-19 149 views
1

我有這樣的XML。我無法把它解析爲使用表oracle XML到數據庫表

<REQMST> 
    <ROW> 
    <REQ_ID>668 </REQ_ID>  
     <RequestDetails> 
     <REQ_DTL_ID>845</REQ_DTL_ID> 
     <INTERFACE_REFNUM>1</INTERFACE_REFNUM> 
     </RequestDetails> 
     <RequestDetails> 
     <REQ_DTL_ID>846</REQ_DTL_ID> 
     <INTERFACE_REFNUM>2</INTERFACE_REFNUM> 
     </RequestDetails>  
    </ROW> 
</REQMST> 

我想它解析爲表

select * 
FROM XMLTABLE('/REQMST/ROW' 
     PASSING 
      xmltype(' 
      <REQMST> 
    <ROW> 
    <REQ_ID>668 </REQ_ID>  
     <RequestDetails> 
     <REQ_DTL_ID>845</REQ_DTL_ID> 
     <INTERFACE_REFNUM>1</INTERFACE_REFNUM> 
     </RequestDetails> 
     <RequestDetails> 
     <REQ_DTL_ID>846</REQ_DTL_ID> 
     <INTERFACE_REFNUM>2</INTERFACE_REFNUM> 
     </RequestDetails>  
    </ROW> 
</REQMST>') 
     COLUMNS 
      --describe columns and path to them: 
      REQ_ID varchar2(20)     PATH './REQ_ID', 
      REQ_DTL_ID varchar2(20)   PATH './RequestDetails/REQ_DTL_ID', 
      INTERFACE_REFNUM varchar2(20)  PATH './RequestDetails/INTERFACE_REFNUM' 
    ) xmlt 
; 

我收到錯誤ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence

它的工作原理,如果只有一個RequestDetails

回答

1

您需要使用與父項相同的技術,再降低一級以獲取RequestDetails標記的詳細信息。

使用的列名xml得到XML作爲XMLType列,然後使用XMLTABLE它來獲得進一步的細節:

試試這個:

select xmlt.req_id, x.REQ_DTL_ID, x.INTERFACE_REFNUM 
FROM XMLTABLE('/REQMST/ROW' 
     PASSING 
      xmltype(' 
      <REQMST> 
       <ROW> 
        <REQ_ID>668 </REQ_ID>  
        <RequestDetails> 
         <REQ_DTL_ID>845</REQ_DTL_ID> 
         <INTERFACE_REFNUM>1</INTERFACE_REFNUM> 
        </RequestDetails> 
        <RequestDetails> 
         <REQ_DTL_ID>846</REQ_DTL_ID> 
         <INTERFACE_REFNUM>2</INTERFACE_REFNUM> 
        </RequestDetails>  
       </ROW> 
      </REQMST>') 
     COLUMNS 
      --describe columns and path to them: 
      REQ_ID varchar2(20)     PATH 'REQ_ID', 
      xml xmltype   PATH 'RequestDetails' 
    ) xmlt, xmltable('/RequestDetails' 
     passing xmlt.xml 
     columns 
      REQ_DTL_ID varchar2(20)   PATH 'REQ_DTL_ID', 
      INTERFACE_REFNUM varchar2(20)  PATH 'INTERFACE_REFNUM' 
    ) x; 

產地:

REQ_ID REQ_DTL_ID INTERFACE_REFNUM 
668  845   1 
668  846   2