XQuery!=带序列的运算符

时间:2015-06-22 20:07:27

标签: xquery marklogic

我遇到了以下问题,语法明智我不知道为什么我会得到我得到的结果。

我有以下FLOWR表达式:

(: example 1 :)
for $attr in $node/@*
where $attr/fn:local-name != ("src", "type", "id")
return $attr

我想到的英文版本是:Get me all the attributes that are not src, type, or id

但是,当我运行它时,会返回每个属性,包括src, type, and id。当我将where语句更改为只有一个元素where $attr/fn:local-name != ("src")时,这会按预期工作 - 返回除src之外的所有属性。很奇怪,它与一个元素比较时有效,但不是三个。

如果我改变了我的逻辑,并做出这样的声明:

(: example 2 :)
for $attr in $node/@*
where $attr/fn:local-name = ("src", "type", "id")
return $attr
(: difference is = instead of != :)

然后我也得到了我期望的结果,这只是3个属性"src", "type", and "id",没有别的。

所以,回到我原来的情况,为了让它按照我期望的方式工作,我必须使用以下声明:

(: example 3 :)
for $attr in $node/@*
where fn:not($attr/fn:local-name = ("src", "type", "id"))
return $attr

这将返回除src, type, and id以外的所有属性。

为什么会发生这种情况?在我看来,示例1 示例3 应该做同样的事情。但是,我无法让示例1 按照我期望的方式工作。

我的问题的xPath等同于:

$node/@*[fn:not(./fn:local-name(.) = ("src", "type", "id"))]

有人可以解释我的想法有缺陷吗?

我正在使用xquery version "1.0-ml"

2 个答案:

答案 0 :(得分:3)

当您说$x != (1, 2, 3)转换为$x does not equal 1, 2, and 3时。如果左侧和右侧的任何值不相等,!=运算符将返回true,因此如果$x1,则仍会返回true,因为{ {1}}不等于$x2

答案 1 :(得分:2)

问题是当您认为通过将=更改为=来反转逻辑时。但这并不是彼此相反的。

当左侧序列中的任何项目等于右侧序列中的任何项目时,

!=为真。当左侧序列中的任何项目与右侧序列中的任何项目不同时,( 1, 2, 3 ) = ( 1, 5 ) (: true :) ( 1, 2, 3 ) = ( ) (: false :) ( 1, 2, 3 ) != ( 1, 5 ) (: true :) 为真。

x=y

正如您所发现的,not(x=y)的反面是not(x=y)

当左侧序列或右侧序列是单身时,根据定义x!=y等于{{1}}。

相关问题