简体   繁体   English

XPath - 'not'和'!='之间的区别

[英]XPath - Difference between 'not' and '!='

Just a quick question as to the difference between xpath's 'not' and '!=' in the following content. 关于xpath的'not'和'!='在以下内容中的区别,只是一个简单的问题。

Taking the XML: 采用XML:

<years>
  <year value="2010"></year>
  <year value="2010"></year>
  <year value="2010"></year>
  <year value="2009"></year>
</years>

I want to select unique years. 我想选择独特的年份。 I have struggled for a while to achieve this, but managed in the end, but in a curious way that I did not expect. 我已经挣扎了一段时间才能实现这一目标,但最终还是得到了管理,但是我以一种奇怪的方式让我没想到。

The following xpath is correct for my intention and returns two unique year nodes of 2009 and 2010. 以下xpath对我的意图是正确的,并返回2009年和2010年的两个独特年节点。

years/year[not(@value = preceding-sibling::year/@value)]

The following only returns the 2009 year node. 以下仅返回2009年节点。

years/year[@value != preceding-sibling::year/@value]

The only difference between them is the != and not operators. 它们之间唯一的区别是!=而不是运算符。 I've pondered on this a while and I can't find a difference that I could satisfactorily explain to anyone else. 我已经思考了一段时间,我找不到可以令人满意地向其他人解释的差异。

Perhaps someone could help. 也许有人可以帮忙。

Cheers 干杯

Steve 史蒂夫

The second example does not work because if you apply it to each of the first 3 nodes, it never matches. 第二个示例不起作用,因为如果将它应用于前3个节点中的每个节点,它永远不会匹配。 For the first <year> , there's no preceding sibling whose value one might try to compare to, so it fails to match. 对于第一个<year> ,没有先前的兄弟,其值可能会尝试与之比较,因此无法匹配。 For the second and third, their preceding node does have the same value, so the non-equality test fails and leads to no match again. 对于第二个和第三个,它们的前一个节点具有相同的值,因此不相等测试失败并导致不再匹配。

The not(...) version works because in the first node, the whole @value = preceding-sibling::year/@value fails due to the lack of a preceding sibling, and this failure in inverted by not , giving you a match on the first node. not(...)版本可以正常工作,因为在第一个节点中,由于缺少前面的兄弟,整个@value = preceding-sibling::year/@value失败,而这个失败因为not反转,给你一个在第一个节点上匹配。

In XPath, a != b and not(a = b) are VERY different 在XPath中, a != bnot(a = b)非常不同

Here's why 这就是原因

From the spec for XPath 1.0: 从XPath 1.0的规范:

If both objects to be compared are node-sets, then the comparison will be true if and only if there is a node in the first node-set and a node in the second node-set such that the result of performing the comparison on the string-values of the two nodes is true. 如果要比较的两个对象都是节点集,那么当且仅当第一个节点集中有一个节点而第二个节点集中有一个节点时才进行比较,以便在该节点上执行比较的结果两个节点的字符串值为true。

that means that (a = b) for node sets is true if there is a match between any element in a and b. 这意味着如果a和b中的任何元素之间存在匹配,则节点集的(a = b)为真。
(a != b) means that some element in a DOES NOT match some element in b. (a!= b)表示DOES中的某些元素与b中的某个元素不匹配。 so for the node sets A = (1, 2), B = (1, 2). 所以对于节点集A =(1,2),B =(1,2)。 BOTH a = b and a != b will return true. 两个a = b和a!= b将返回true。

In your case what's happening is that (2010 != empty set) is always false, while 在你的情况下,发生的事情是(2010 != empty set)总是假的,而
not (2010 = empty set) is always true. not (2010 = empty set)始终为真。 Think about the matching rules as above. 考虑上面的匹配规则。

我不是xpath的专业人士,但我认为'not'重新计算包含事物的倒置值,而!=返回两个可比较事物之间的比较值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM