2015-06-22 29 views
4

我遇到以下問題,並且語法明智我不知道爲什麼我得到我得到的結果。XQuery!=運算符與序列

我有以下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

問題是,當您認爲通過將!=更改爲=來反轉邏輯時。但這不是彼此相反的。

=當左側序列中的任何項目等於右側序列中的任何項目時爲true。 !=在左側序列中的任何項目與右側序列中的任何項目不同時爲真。

(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

3

當你說$x != (1, 2, 3)轉換爲$x does not equal 1, 2, and 3。如果左側和右側的任何值不相等,則!=運算符將返回true,因此如果$x1,則仍然返回true,因爲$x不等於23