koopajah
koopajah

Reputation: 25582

XQuery to change attribute value and return previous one

I'm trying to update an attribute value of a node and return its previous value all in one query and I can't find a way to do it. I'm using BaseX as my XML/XQuery database.

For now I've tried doing this:

/Root/Elem/properties/property[@id='17']/@format,
replace value of node /Root/Elem/properties/property[@id='17']/@format with 'URL'

and also this:

for $prop in /Root/Elem/properties/property[@id='17']
    let $format := $prop/@format
    return (replace value of node $prop/@format with 'URL', $format)

And multiple other tests but they all lead to the following error:

List expression: no updating expression allowed.

Is it a limitation of BaseX or is it not possible in XQuery?

Upvotes: 1

Views: 2499

Answers (1)

Leo Wörteler
Leo Wörteler

Reputation: 4241

XQuery Update does not allow returning results from an updating query. You can however use BaseX's proprietary update:output($seq) function to do that:

for $prop in /Root/Elem/properties/property[@id='17']
let $format := $prop/@format
return (replace value of node $format with 'URL', update:output($format))

Upvotes: 3

Related Questions