首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >If-语句:不完全FLWOR表达式

If-语句:不完全FLWOR表达式
EN

Stack Overflow用户
提问于 2018-04-15 20:40:57
回答 1查看 613关注 0票数 1

我在XQuery编辑器中有下面的BaseX代码,但是它在if-子句上出错,声明

不完全FLWOR表达式:期待‘返回’

我试图使用的代码:

代码语言:javascript
复制
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的代码中有另一个错误。)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-16 09:06:59

我认为这里真正的问题是您正在使用if-表达式。

代码语言:javascript
复制
if (file:exists($dir) and file:is-dir($dir))
          then () else file:create-dir($dir) 

为了达到副作用,而不是为了返回一个结果。虽然外部函数(如file:create())有可能产生副作用,但这并不是XQuery设计的工作方式,因此您需要非常小心地使用这些外部函数。不仅如此,关于什么工作和什么不工作的详细规则可能因XQuery处理器的不同而有所不同。

此外,当然,您的查询必须满足语法,FLWOR表达式的语法说明它由一系列子句组成,每个子句都是for、let、where、order-by、.。或者退货条款。所以你的“如果”表达错了。

我不知道BaseX,但我认为以下几点可能会奏效:

代码语言:javascript
复制
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
)  
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49846582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档