Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different xquery result between Let and For when using "every" clause

Tags:

xml

xquery

I have the following Xquery:

let $x := <a>
           <i>false</i>
          </a>
return
    if (every $t in $x/a satisfies $t/i eq "true")
    then $x
    else <nothing/>

How I interpret this is, return $x if all <a> have an <i> that has the word "true". However, this always return $x.

In contrast, if I have the following XML doc:

<root>
  <a><i>false</i></a>
</root>

and the following query:

for $x in /root
return
    if (every $t in $x/a satisfies $t/i eq "true" )
    then $x
    else <nothing/>

It will return <nothing/> when <i> contains false and will return $x when <i> is "true"

My questions are:

  1. Why does the query with "let" behave the way it does?
  2. Why does the query with "for" behave differently?
like image 823
user3617530 Avatar asked Nov 24 '25 22:11

user3617530


1 Answers

The key difference is the existence of a root element assigned to $x in the second example, but not in the first.

In your first example the root element is <a>, but in your quantifier expression you write every $t in $x/a. $x/a selects child a elements of <a>, and there are none (only i). So $x/a evaluates to empty. The expression is reduced to every $t in () satisfies $t/i eq "true". Since empty is assigned to $t the full quantifier expression evaluates to true.

like image 117
wst Avatar answered Nov 27 '25 15:11

wst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!