XPath - 'not' 和 '!=' 之间的区别 [英] XPath - Difference between 'not' and '!='

查看:61
本文介绍了XPath - 'not' 和 '!=' 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于以下内容中 xpath 的 'not' 和 '!=' 之间的区别只是一个简单的问题.

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

采用 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.

以下 xpath 符合我的意图,并返回 2009 和 2010 两个唯一的年份节点.

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)]

以下仅返回 2009 年节点.

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.

也许有人可以帮忙.

干杯

史蒂夫

推荐答案

第二个示例不起作用,因为如果将其应用于前 3 个节点中的每一个,它永远不会匹配.对于第一个 <year>,没有可能尝试比较其值的前一个兄弟,因此无法匹配.对于第二个和第三个,它们前面的节点确实具有相同的值,因此不等式测试失败并再次导致不匹配.

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.

not(...) 版本有效,因为在第一个节点中,由于缺少前面的兄弟节点,并且这个失败被 not 反转,让你在第一个节点上匹配.

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.

这篇关于XPath - 'not' 和 '!=' 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆