XPath operator “!=”. How does it work?

时光毁灭记忆、已成空白 提交于 2019-11-28 07:15:26

Recommendation: Never use the != operator to compare inequality where one or both arguments are node-sets.

By definition the expression:

$node-set != $value

evaluates to true() exactly when there is at least one node in $node-set such that its string value is not equal to the string value of $value.

Using this definition:

$empty-nodeset != $value 

is always false(), because there isn't even a single node in $empty-nodeset for which the inequality holds.

Solution:

Use:

not($node-set = $value)

Then you get all results true(), as wanted.

From the XPath spec:

If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true.

This means that if the node-set is empty (as in your cases C and D), the result of the boolean expression will be false, since there is no node to which the inequality can apply.

You can work around this behaviour and get the result you want using an expression like:

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