为什么这个 XQuery return 是一个空序列,而我期待的是 5?

Why is this XQuery return an empty sequence while I'm expecting 5?

我对下面的查询感到困惑,我希望结果应该是 5 但它 returns 一个 空序列 .

let $num := 5
let $num1 := ()
let $add := $num + $num1
return $add

我不知道 Marklogic XQuery 与 W3C XQuery 标准有多接近,但在该标准中算术表达式的规则 https://www.w3.org/TR/2010/REC-xquery-20101214/#id-arithmetic 清楚地说

The first step in evaluating an arithmetic expression is to evaluate its operands. The order in which the operands are evaluated is implementation-dependent.

Each operand is evaluated by applying the following steps, in order:

Atomization is applied to the operand. The result of this operation is called the atomized operand.

If the atomized operand is an empty sequence, the result of the arithmetic expression is an empty sequence

所以空序列是定义的结果,因为一个操作数是空序列。

也许 sum() 函数 let $add := sum(($num, $num1)) 更符合您的需求。