I have been work on the xDB for some time recently. The xDB has some restriction on the place where you can put the xquery update functions. It’s quite confusing if you don’t know this. I posted a question on the EMC community, and thanks to Martin Probst, I got the answer. Please refer to this page for the original discussion.

xDB tries to adhere to the XQuery Updates specification. That means you can only use updating functions within updating functions, which you have to explicitly declare:

declare updating function local:foo($node) {   xhive:remove($node)
};
local:foo(...)


Generally speaking, updating expressions may only occur in a call path that will be directly returned as a result, i.e. in the return clause, or in a sequence expressions (remove node ..., other expression), and in updating functions in the same places. They are not allowed within expressions that get assigned to variable.



The specification has been a bit sketchy at the time this was implemented for 9.0. Future versions of xDB will drop this requirement as it seems not very helpful in general, and allow to enable a strict W3C mode on demand.



Beside that, you can return values from updating functions and assign them to variables, though according to the W3C specification that is a bug in xDB. That means your query might not be compatible across XQuery implementations.