I have the following XQuery code in the BaseX editor, but it errors on the if-clause, stating
Incomplete FLWOR expression: expecting 'return'
The code that I am trying to use:
import module namespace functx = 'http://www.functx.com';
declare namespace file = "http://expath.org/ns/file";
let $root := 'E:\basex-index-testing\sonar-small\index'
let $ds := file:dir-separator()
(: get files in sonar-small database :)
for $f in db:list('sonar-small')
(: remove *.xml from doc name :)
let $corpus := substring($f, 1, string-length($f) - 4)
for $alpino in db:open('sonar-small', $f)/treebank/alpino_ds
(: make sure tree has sentence element :)
where count($alpino/sentence) > 0
let $sentenceId := data($alpino/@id)
for $node in $alpino//node
(: make sure there are less than 500 descendants,
less than 100 and more than 0 children :)
where count($node//node) < 500 and count($node/node) > 0
and count($node/node) < 100
let $catTop := data($node/@cat)
(: create indexing pattern based on node's direct children :)
let $childrenRelCat := ()
for $child in $node/node
let $childRel := data($child/@rel)
(: use children's cat or pt attribute, default to '' :)
let $childCat := data($child/(@cat, @pt, '')[1])
(: concatenate childrenRelCat sequence (append to list) :)
let $childrenRelCat := ($childrenRelCat,
string-join(($childRel, $childCat), '%'))
let $bf := string-join(functx:sort($childrenRelCat), '_')
let $sent := <tree id="{$sentenceId}">{$node}</tree>
let $dir := concat($root, $ds, $catTop)
(: this if-clause throws an error: missing return statement,
incomplete FWLOR:)
if (file:exists($dir) and file:is-dir($dir))
then ()
else file:create-dir($dir)
(: append subtree to pattern-file :)
file:append($dir || $bf || '-index.xml', $sent)
(: doesn't have to return anything, but FWLOR demands it... :)
return $f
It seems that I am missing something crucial in the way how XQuery evaluates expressions or expects them to be ordered. What is wrong in the code above?
(This question is titled almost identically, but the answer provided there does not help as there was another error in the code of that OP.)