我在 BaseX 编辑器中有以下 XQuery 代码,但它在 if 子句上出错,指出
不完整的FLWOR表达:期待"回归">
我尝试使用的代码:
import module namespace functx = 'http://www.functx.com';
declare namespace file = "http://expath.org/ns/file";
let $root := 'E:basex-index-testingsonar-smallindex'
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
似乎我在 XQuery 如何计算表达式或期望它们被排序的方式中遗漏了一些关键的东西。上面的代码有什么问题?
(这个问题的标题几乎相同,但那里提供的答案没有帮助,因为该 OP 的代码中存在另一个错误。
我认为这里真正的问题是你使用的是 if 表达式
if (file:exists($dir) and file:is-dir($dir))
then () else file:create-dir($dir)
为了达到副作用,而不是为了返回一个结果。尽管像 file:create-dir(( 这样的外部函数可能会产生副作用,但它并不是 XQuery 真正设计的工作方式,因此您需要非常小心如何使用此类外部函数。不仅如此,关于什么有效和什么无效的详细规则可能因一个 XQuery 处理器而异。
此外,当然,您的查询必须满足语法,FLWOR 表达式的语法说它由一系列子句组成,每个子句都是 for、let、where、order-by、....或返回条款。所以你的"如果"表达放错了地方。
我不知道 BaseX,但我认为以下内容可能会起作用:
let $dir := concat($root, $ds, $catTop)
return (
if (file:exists($dir) and file:is-dir($dir))
then ()
else file:create-dir($dir),
file:append($dir || $bf || '-index.xml', $sent),
$f
)