If-statement: 不完整的 FLWOR 表达式

If-statement: Incomplete FLWOR expression

我在 BaseX 编辑器中有以下 XQuery 代码,但它在 if-clause 上出错,显示

Incomplete FLWOR expression: expecting 'return'

我尝试使用的代码:

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 计算表达式或期望它们被排序的方式中,我似乎遗漏了一些重要的东西。上面的代码有什么问题?

的标题几乎相同,但那里提供的答案无济于事,因为该 OP 的代码中存在另一个错误。)

我认为这里真正的问题是你使用了 if 表达式

if (file:exists($dir) and file:is-dir($dir))
          then () else file:create-dir($dir) 

为了达到副作用,而不是为了return一个结果。尽管像 file:create-dir() 这样的外部函数可能有副作用,但这并不是 XQuery 设计的真正工作方式,因此您需要非常小心地使用这些外部函数。不仅如此,关于什么有效什么无效的详细规则可能因 XQuery 处理器而异。

此外,当然,您的查询必须满足语法,FLWOR 表达式的语法说它由一系列子句组成,每个子句是 for、let、where、order-by、 .... 或 return 子句。所以你的 "if" 表达式放错了地方。

我不知道 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
)