XPath - Difference between 'not' and '!='

天涯浪子 提交于 2021-02-18 22:24:05

问题


Just a quick question as to the difference between xpath's 'not' and '!=' in the following content.

Taking the 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.

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

The following only returns the 2009 year node.

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


回答1:


The second example does not work because if you apply it to each of the first 3 nodes, it never matches. For the first <year>, there's no preceding sibling whose value one might try to compare to, so it fails to match. 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.




回答2:


In XPath, a != b and not(a = b) are VERY different

Here's why

From the spec for 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.

that means that (a = b) for node sets is true if there is a match between any element in a and b.
(a != b) means that some element in a DOES NOT match some element in b. so for the node sets A = (1, 2), B = (1, 2). BOTH a = b and a != b will return true.

In your case what's happening is that (2010 != empty set) is always false, while
not (2010 = empty set) is always true. Think about the matching rules as above.




回答3:


i am no pro on xpath but i think the 'not' retunrns the inverted value of the containing thing and the != returns the value of the comparision between two compareable things



来源:https://stackoverflow.com/questions/2273853/xpath-difference-between-not-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!