使用"every"子句时,Let和For之间的xquery结果不同

use*_*530 3 xml xquery

我有以下Xquery:

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

我如何理解这一点,$x如果所有人<a>都有一个<i>单词"true" ,则返回.但是,这总是回归$x.

相反,如果我有以下XML文档:

<root>
  <a><i>false</i></a>
</root>
Run Code Online (Sandbox Code Playgroud)

以及以下查询:

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

它将<nothing/><i>包含false时返回$x,<i>并在"true" 时返回

我的问题是:

  1. 为什么带"let"的查询的行为方式如此?
  2. 为什么"for"的查询行为不同?

wst*_*wst 5

关键的区别在于第二个示例中存在分配给$ x的根元素,但在第一个示例中则不存在.

在您的第一个示例中,根元素是<a>,但在您编写的量词表达式中every $t in $x/a.$x/a选择子a元素<a>,并且没有(仅i).所以$x/a评估为空.表达式减少到every $t in () satisfies $t/i eq "true".由于将空分配给$t完整量词,因此表达式的计算结果为true.