3
votes

I want to change my current return statement in my XQuery, which is :

return
 <p>XML File Stored Successfully</p>

I want to make this return statement capable of handling the situation if there is an error, then return an error code to the user, if there is no error, then return the message above to the user.

I think an if-else construct should be placed under return for my purpose. But I really have no idea what condition should be there for the if, could experts help a little? Thanks in advance.

1
Well, the condition should be whether or not there was error. Are you setting a variable somewhere that would indicate whether or not an error occurred? What sort of error are you even looking to catch? - Chris Marasti-Georg
@Chris, I am trying my best to think of using such a variable, but not many clues. - Kevin
Well what is the rest of your XQuery doing, that an error might occur while still allowing the return statement to process? - Chris Marasti-Georg

1 Answers

3
votes

The XQuery 3.0 working draft introduces try-catch expressions, which might be what you are looking for:

declare namespace err = "http://www.w3.org/2005/xqt-errors";

let $x := "string"
return
  try {
    $x cast as xs:integer
  } catch * {
    $err:code (: this variable contains the error code :)
  }