递归函数返回boolean,里面有for循环

时间:2011-09-20 17:28:48

标签: xquery

我的数据是二叉树,将检查每个孩子,如果找到我想要的数据则返回true,否则,它会继续查找。 在某种程度上,我想要返回变量@exists或者其他东西..任何人都可能有我的问题的解决方案。我在想这样的事情,但我无法让它发挥作用! (代码片断)

declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean {
    let $exists := fn:false()
    for $x in ...
    return
        if .. then
            set exists to fn:true()
        else
            set exists to exists OR local:test($x,$topic)

    return @exists in some way  
};

3 个答案:

答案 0 :(得分:2)

这是XQuery quantified expression的一个案例。使用它,您的功能转换为

declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean
{
  some $x in ...
  satisfies
    if (..) then
      fn:true()
    else
      local:test($x,$topic)
};

答案 1 :(得分:1)

正如已经提到的,XQuery是一种功能语言。你不能只设置变量并返回它。您的查询可以重写为:

declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean {
    exists(for $x in ...
           where (: here is condition expression on $x :)
           return $x)
};

如果exists(Expr)的值不是空序列,则函数true返回Expr;否则,函数返回false

在这种情况下exists如果true符合指定条件,则会返回$x

答案 2 :(得分:0)

您无法更改xquery中的变量值。

你的整个功能不只是这个:

declare function local:test($topic as xs:integer) as xs:boolean {
     ... OR local:test($topic/...)
};
相关问题