Thanks for the example.
On Mon, Nov 27, 2017 at 8:49 PM, E. Wray Johnson wray.johnson@gmail.com wrote:
I cannot get db:output to work. Error is [XPST0008] Undefined variable $c for the following sample code:
There are two things to consider:
• You need to use parentheses after the return clause. • The official XQuery Update spec does not allow the return clause of copy/modify/return to be updating (I think it shouldn’t actually cause problems to relax this rule, so maybe I will allow it in BaseX soon). You can wrap this expression into an additional FLWOR expression.
This should work:
let $doc := ( copy $c := element test { element id { }} modify ( replace value of node $c/id with generate-id($c) ) return $c ) return ( db:output($doc/id), insert node $doc into db:open('sample', 'tests') )
However, the following alternative might be a better solution, because the ids created via generate-id() won’t be unique if you restart BaseX:
let $doc := element test { element id { random:uuid() } } return ( db:output($doc/id), insert node $doc into db:open('sample', 'tests') )
If you want to have shorter ids, you could add a meta document in your database and update it every time you add a new document. In the following example, the id will be included in the name of the document. Just a tiny example:
Initialization:
db:create('db', <meta><id>0</id></meta>, 'meta.xml')
Adding new document:
let $db := 'db' let $meta-doc := db:open($db, 'meta.xml') update { for $id in meta/id/text() return replace node $id with $id + 1 } let $id := string($meta-doc/meta/id) let $new-doc := element test { element id { $id } } return ( db:replace($db, 'meta.xml', $meta-doc), db:replace($db, 'doc' || $id || '.xml', $new-doc), db:output($id) )
Best, Christian
copy $c := element test { element id { }} modify ( replace value of node $c/id with generate-id($c) ) return db:output ($c/id), insert node $c into db:open('sample', 'tests')