when to use != or not() in XPath

Simple question. When should I use not() and when should I use !=. Example: //General.User[active != true()] or //General.User[not(active = true())] Is there any technical difference between both solutions?
4 answers

The difference between them is that "!=" compares two values and if they are the same it returns false, if not it returns true. "not()" checks whether certain logic holds true and if so it returns false, if not it returns true. In other words "not()" is a logical operator while "!=" is a comparison operator.

To quote some random page on the internet:
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Logical operators are used to determine the logic between variables or values.

Say x = false and y = false:

not(x and y) returns true;
while x != y returns false;

EDIT (added): Compare: "not(x=100)" to "x!=100" and it should be obvious why using != is superior for comparing two values.

In your example I am not sure it matters, maybe there is a performance difference but for such a simple case it will be negligible. Even so, in your example I would use neither and opt for //General.User[active = false()] instead.


If applicable, != should always be preferred above 'not()', because not tells the database to inverse a result set (in other words 'please provide me all rows that did not match the inner query) and '!=' just compares two values, which is way cheaper.


See my answer to this question:



I would choose for readability.

Not can include several expressions and != is part of one expression as an operator.