Skip to main content
XMLDir

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

XPath operator

Union |

Combines two node-sets into one, in document order and without duplicates. It only works on node-sets.

The union operator merges node-sets. The result is in document order with duplicates removed, regardless of the order the operands were written in.

It is the only way to select alternatives in XPath 1.0 — there is no | inside a name test, so you cannot write //(a|b).

Syntax
node-set | node-set
Returns
node-set
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(//price | //line)on order.xmlRun
    6
  • count(//customer | //note)on order.xmlRun
    2
  • count(//line | //line)on order.xmlRun
    3

    Duplicates are removed, so a set unioned with itself is unchanged.

Run your own expression

What catches people out

  • Both operands must be node-sets. 'a' | 'b' is a type error, not a string alternation.
  • The result is always in document order, so you cannot use | to control ordering.
  • There is no alternation inside a step: write //a | //b, not //(a|b).

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.