我在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-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
似乎我错过了XQuery如何评估表达式或期望它们被排序的关键。上面的代码有什么问题?
(This question的标题几乎相同,但由于该OP的代码中存在另一个错误,因此提供的答案没有帮助。)
答案 0 :(得分:2)
我认为这里真正的问题是你正在使用if-expression
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,.. ..或退货条款。那么你的&#34;如果&#34;表达是错误的。
我不了解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
)