Markus,

 

There are a couple of syntax issues with your code shown below:

 

  1. You have a “,” (comma) at the end of the $let $my_data line.
  2. The assignment following the “,” on that line needs to be the return statement

 

This aspect of XQuery syntax (when to use commas, when to construct explicit sequences with wrapping parens) can be a bit challenging to internalize. If you haven’t already, I’d highly recommend reading _XQuery for Humanists_ from A&M press [2]. I found it to be a really good introductory text that covers all the details with practical examples.

 

This rework runs for me in the BaseX GUI:

 

(: Start of query :)

declare %updating function local:add_document($items as item()*)

as empty-sequence()

{

db:add("Test_DB", $items, "Test.xml")

};

 

let $my_data as element() := <root><contents>My document</contents></root>

return local:add_document($my_data)

(: end of query :)

 

You can also use the BaseX MIXEDUPDATES option to turn off the XQuery update restrictions to allow you to mix updating and non-updating expressions in a single query, although you should only do this if you fully understand the implications, in particular, that updates may not be applied synchronously with non-updating instructions, as explained here at [1].

 

[1] https://docs.basex.org/wiki/XQuery_Update#Pending_Update_List

[2] https://www.tamupress.com/book/9781623498290/xquery-for-humanists/

_____________________________________________

Eliot Kimber

Sr Staff Content Engineer

O: 512 554 9368

M: 512 554 9368

servicenow.com

LinkedIn | Twitter | YouTube | Facebook

 

From: Markus Elfring <Markus.Elfring@web.de>
Date: Wednesday, May 18, 2022 at 2:23 AM
To: Eliot Kimber <eliot.kimber@servicenow.com>
Cc: basex-talk@mailman.uni-konstanz.de <basex-talk@mailman.uni-konstanz.de>
Subject: Re: [basex-talk] Adding another document from XQuery evaluation

[External Email]


> You can add documents to the database without first writing them to the file system[1]:

Thanks for such information.


> This needs to be done in an updating function or …

I am looking for further advices also according to XQuery script variants
like the following.


declare %updating function local:add_document($items as item()*)
as empty-sequence()
{
let $result := db:add("Test_DB", $items, "Test.xml")
return ()
};

let $my_data as element() := <root><contents>My document</contents></root>,
    $result := local:add_document($my_data)
return ()


Test result:
[XUST0001] let: no updating expression allowed.


let $my_data as element() := <root><contents>My document</contents></root>,
    $add_document := %updating function($items) {db:add("Test_DB", $items, "Test.xml")}
return ($add_document($my_data))


Test result:
[XPTY0004] Function is updating: $add_document.

https://urldefense.com/v3/__https://docs.basex.org/wiki/XQuery_Update*User-Defined_Functions__;Iw!!N4vogdjhuJM!Fy9Cp90xL1ZsEl9TqGgJQ3PgRIIfCx5mNMBfC2fXspz6fJUXIGjWforMVJhv_358UEriAcxn-cjW7Xe8aPUq1ukLx0i2Zbw$

How should the affected components be adjusted further according to
the development software “BaseX 9.7.2”?

Regards,
Markus