Recursive function returning boolean, with a for loop inside
My data is a binary tree, and will check through every child, returning true if it finds the data i want, if not, it keeps looking for it. In some way i want to return the variable @exists or something.. Well anyone might have a solution for my problem. I was thinking something like this but i couldn't get it to work! (code-snippet)
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
};
This is a case for an XQuery quantified expression. Using that, your function translates to
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)
};
As it was already mentioned XQuery is a functional language. You can't just set variable and return it. Your query can be though rewritten like:
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)
};
Function exists(Expr)
returns true
if the value of Expr
is not the empty sequence; otherwise, the function returns false
.
In this case exists
returns true
if there is $x
which meets specified condition.
You cannot change the values of variables in xquery.
Is your whole function not just this:
declare function local:test($topic as xs:integer) as xs:boolean {
... OR local:test($topic/...)
};
精彩评论