Skip to main content
XMLDir

Search 169 pages — formats, namespaces, parser messages, schema errors and tools.

XPath operator

Comparison = and !=

Comparison against a node-set is existential, which makes != mean something other than 'not ='.

When one side is a node-set, = is true if *any* node in it satisfies the comparison. That rule is reasonable on its own and produces a genuinely surprising consequence: a != b is not the same as not(a = b).

For a set containing 2, 1 and 1: @qty != '1' is true, because some node differs. not(@qty = '1') is false, because some node matches. Both statements are correct and they disagree.

Syntax
object = object
Returns
boolean
Kind
Operator
Version
XPath 1.0 (libxml2)

Worked examples

Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.

  • count(//line[@qty = '1'])on order.xmlRun
    2
  • //line/@qty != '1'on order.xmlRun
    true

    True: at least one qty differs from 1.

  • not(//line/@qty = '1')on order.xmlRun
    false

    False: at least one qty equals 1. The opposite answer to the line above.

Run your own expression

What catches people out

  • a != b is not not(a = b) when a node-set is involved. Use not(… = …) when you mean negation.
  • Comparing two node-sets is true when any pair matches, which is rarely what anyone intends.
  • A string compared to a number converts the string. '01' = 1 is true.

The sample documents

Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.

order.xml
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
  <customer country="NO">Ada Lovelace</customer>
  <lines>
    <line sku="WIDGET-1" qty="2"><price>14.50</price></line>
    <line sku="WIDGET-2" qty="1"><price>39.00</price></line>
    <line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
  </lines>
  <note>  Deliver after 09:00  </note>
</order>

Related

All 34 XPath entries

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.