Hi Bogdan,
- XQuery is functional language and updating function can return empty sequence. How can I count and return result how many nodes was inserted or updated ?
BaseX provides the update:output function for that purpose [1]. A solution could look as follows:
declare %updating function cfv:upsertVehicles( $vehicles as element(car)* ) as empty-sequence() { let $db := db:open("db")/cars let $existing := $vehicles[$db/car/vin = vin] return ( (: replace existing vehicles :) for $updated in $existing let $old := $db/car[vin = $updated/vin] return replace node $old with $updated, (: insert new vehicles :) insert node ($vehicles except $existing) into $db, (: return count :) update:output(count($vehicles)) ) };
I have similar problem with error handling. How can I catch and return any error.
Due to the semantics of XQuery Update, logical errors will be raised before your updates are eventually executed [2]. No updates will be performed if the execution is expected to fail.
Best, Christian
[1] http://docs.basex.org/wiki/Update_Module#update:output [2] http://docs.basex.org/wiki/XQuery_Update#Error_Messages