try catch with return?

Friday, 23 November 2012 · 2 messages
BR
bryan rasmussen23 Nov 2012 12:34 #
Hi, I'm getting the following: Error: Stopped at line 1, column 73: [XPST0003] Expecting 'where', 'order' or 'return' expression.
Query: for $doc in db:open('dk')  let $dates := $doc//DATO for $date
in $dates  try{ return <gooddate>{xs:date($date)}</gooddate> } catch
*{ return <baddate docid="{$doc/*/@ID}">{$date}</baddate>}
I wanted to basically to return all DATO elements with document ids that were not valid xs dates, and figured a try catch with returns in it were the best option. Is that not allowed or have I made some basic error? If it's not allowed how would you handle it? Note it is not really realistic to do something like catch these errors in validation since they've already been past the validation process, first I'd have to do a lot of work to write a schema that handled the document structure since there are a lot of mixed content possibilities (or add new validation language etc.), second I'd have to have it in my validation pipeline and the several hundred thousand large documents would have to be run through the pipeline again just to find a few thousand malformed dates that anyway are currently not used for anything important (but of course if we make them valid date then they can start being used for stuff) at any rate handling it with XQuery seems the best solution. Thanks, Bryan Rasmussen
LW
Leo Wörteler23 Nov 2012 12:46 #
Hi Bryan,
Quoted text · bryan rasmussen · 7 lines
Am 23.11.2012 12:34, schrieb bryan rasmussen: > Error: Stopped at line 1, column 73: > [XPST0003] Expecting 'where', 'order' or 'return' expression. > > Query: for $doc in db:open('dk') let $dates := $doc//DATO for $date > in $dates try{ return <gooddate>{xs:date($date)}</gooddate> } catch > *{ return <baddate docid="{$doc/*/@ID}">{$date}</baddate>}
your return keywords are in the wrong spot, there should be one before the try and none inside:
  for $doc in db:open('dk')
  let $dates := $doc//DATO
  for $date in $dates
  return
    try{
      <gooddate>{xs:date($date)}</gooddate>
    } catch * {
      <baddate docid="{$doc/*/@ID}">{$date}</baddate>
    }
Hope that helps, cheers, Leo

⚡Generated with XQuery