XPath - Difference between 'not' and '!='
以下内容中的xpath \\'not \\'和\\'!= \\'之间的区别只是一个简单的问题。
采用XML:
1 2 3 4 5 6 | <years> <year value="2010"></year> <year value="2010"></year> <year value="2010"></year> <year value="2009"></year> </years> |
我想选择唯一的年份。我已经为实现这一目标而奋斗了一段时间,但最终还是设法做到了,但是以一种我没想到的奇怪的方式。
以下xpath符合我的意图,并返回两个唯一的年份节点2009和2010。
1 | years/year[not(@value = preceding-sibling::year/@value)] |
以下仅返回2009年节点。
1 | years/year[@value != preceding-sibling::year/@value] |
它们之间的唯一区别是!=而不是运算符。我已经考虑了好一阵子,但找不到我可以令人满意地向其他人解释的区别。
也许有人可以提供帮助。
干杯
史蒂夫
在XPath中,
这就是为什么
根据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.
这意味着如果a和b中的任何元素匹配,则节点集的(a = b)为true。
(a!= b)表示DO中的某些元素与b中的某些元素不匹配。因此对于节点集A =(1,2),B =(1,2)。 a = b和a!= b都将返回true。
在您的情况下,发生的情况是
第二个示例不起作用,因为如果将其应用于前三个节点中的每个节点,则它永远不会匹配。对于第一个
我不是xpath上的专家,但我认为'not'会重新调整包含事物的反转值,而!=返回两个可比较事物之间的比较值