0

Using XQuery in BaseX 7.7.2, I want to fill a template node with some variable values and insert it into a document, and only return the newly created node, not the context node I inserted into.

(: define an "auto-increment" id :)
let $nextId := if( exists( //company/id ) ) then max( //company/id ) + 1 else 1

(: fill the template node (a simplified example node) :)
let $company := <company><id>{ $nextId }</id></company>

(: insert the new node :)
return insert node $company into /companies

This works. However, I don't want it to return /companies but merely the new node $company.

If I alter the query to the following, however, it doesn't work anymore.

(: insert the new node; does not work :)
insert node $company into /companies

(: only return the newly created node :)
return $company

Why am I not allowed to perform intermediate updating operations, before returning something, in FLWOR statements? And why are return statements mandatory in FLWOR expressions anyway?


Edit:

Upon inspection of this XQuery grammar documentation, it appears that perhaps I shouldn't be using a FLWOR expression to begin with at all, but simply use declare variable statements in stead of let statements. However, in that case I still don't know how to return the newly created node either, if explicitly returning something without utilizing FLWOR expressions is possible at all, to begin with.

By default it appears that any updating statement (either with or without FLWOR) simply returns the complete document as a result. As I expect this document to potentially become very large at some point, I don't want this to be sent over the wire with every updating statement.

As you can tell, I'm kind of having trouble understanding the updating/returning concepts in the XQuery language. I hope someone can shed some light on these issues.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106

0 Answers0