2016-12-02 39 views
0

我有以下情形:XQuery的錯誤處理

XQUERY:

declare namespace xs = "http://www.w3.org/2001/XMLSchema"; 


declare variable $udgHeader external; 


let $msg-name := upper-case($udgHeader/msg-name/text()) 
let $recipient := upper-case($udgHeader/recipient/text()) 

return 
<recipients> 
    <recipient> 
     <dest> 
     { 
      if (($msg-name = "ARS_ISTP") and ($recipient = "ISTP")) then 

       'IstpArs' 

      else if (($msg-name = "ARS_ESM") and ($recipient = "ESM")) then 

       'EsmArs' 

      else 


       error(xs:QName('fase'), concat("Unknown msg-name/recipient combination ['", $msg-name,"'/'", $recipient, "']! Please check fase recipient list.")) 

     } 
     </dest> 
    </recipient> 
</recipients> 

我進來的XML將拋出錯誤。

此錯誤我想用Java(負測試)與文件(預期結果)進行比較。

預期結果內容:

Unknown msg-name/recipient combination ['ARS_XYZ'/'ARS']! Please check fase recipient list. 

我的問題是,我得到這個錯誤:

org.apache.xmlbeans.XmlRuntimeException: weblogic.xml.query.exceptions.XQueryUserException: line 29, column 5: fase: Unknown msg-name/recipient combination ['ARS_XYZ'/'ESM']! Please check fase recipient list. 

我該如何處理此錯誤,以配合我預期的結果?

回答

1

當您調用error()會發生什麼的細節有點依賴於您的處理器API,但在Java環境中,我期望您的查詢處理器的調用以異常而不是正常結果退出。如果你想得到一個正常的結果,那麼返回一些你認爲是錯誤結果的元素(或其他對象),而不是調用error()。

在3.0/3.1中,您可以通過在發生錯誤時調用error()來實現此目的,然後使用查詢頂級的try/catch來捕獲它。