1

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  
};
Johan
  • 11
  • 2
  • You may be interested to look at my implementation of the Binary Search Tree data structure in XQuery 3.0 -- here: http://dnovatchev.wordpress.com/2011/05/31/the-binary-search-tree-data-structurehaving-fun-with-xpath-3-0/ – Dimitre Novatchev Sep 22 '11 at 13:36

3 Answers3

2

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)
};
Gunther
  • 5,146
  • 1
  • 24
  • 35
1

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.

Shcheklein
  • 5,979
  • 7
  • 44
  • 53
0

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/...)
};
Oliver Hallam
  • 4,242
  • 1
  • 24
  • 30