【问题标题】:XQuery != operator with a sequence带有序列的 XQuery != 运算符
【发布时间】:2015-09-08 10:22:57
【问题描述】:

我遇到了以下问题,语法方面我不确定为什么会得到我得到的结果。

我有以下 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 != :)

然后我也得到了我期望的结果,这只是 "src", "type", and "id" 的 3 个属性,仅此而已。

所以,回到我原来的案例,为了让它按我期望的方式工作,我必须使用以下语句:

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

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

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

我的问题的 xPath 等效项类似于:

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

谁能解释我的想法哪里有问题?

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

【问题讨论】:

    标签: xquery marklogic


    【解决方案1】:

    问题在于,当您认为通过将 != 更改为 = 来反转逻辑时。但两者并不相反。

    = 当左侧序列中的任何项目等于右侧序列中的任何项目时为真。当左侧序列中的任何项目与右侧序列中的任何项目不同时,!= 为真。

    ( 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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-27
        • 1970-01-01
        • 1970-01-01
        • 2014-05-28
        • 2022-09-30
        • 1970-01-01
        • 1970-01-01
        • 2016-11-17
        相关资源
        最近更新 更多