XPath Union Operator Example

In this article we will learn about XPath Union operator.

1. Introduction

XPath is a syntax for defining parts of an XML document and is a W3C recommendation. XPath uses path expressions to navigate in XML documents and contains a library of standard functions.
XPath is a major element in XSLT and in XQuery. XPath can be used to navigate through elements and attributes in an XML document. XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system. XPath expressions can be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.

2. Union (|) Operator

XML Path Language (XPath) supports the set operation |. The |, or union, operator returns the union of its two operands, which must be node-sets. For example, //company | //employee returns a node-set that combines all the //company nodes and all the //employee nodes. Multiple union operators can be chained together to combine multiple node-sets. For example, //company | //employee | //sales | //consultant returns a node-set containing all //company , //employee , //sales , and //consultant elements. The union operator preserves document order and does not return duplicates.

Let us look at one of the example to understand this in more detail. Let’s take an example xml as below:


<?xml version="1.0"?>
  <a attr1="1">
    <a attr1="2" attr2="3">

The following XSLT style sheet selects all the <a> elements whose a attribute is equal to 2, plus those <a> elements that have no attributes.


<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

  <!-- Suppress text nodes not covered in subsequent template rule. -->
  <xsl:template match="text()"/>

  <!-- Handles a generic element node. -->
  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="*|@*" />
      <xsl:if test="text()">
        <xsl:value-of select="."/>

  <!-- Handles a generic attribute node. -->
  <xsl:template match="@*">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>

  <xsl:template match="/example">
    <xsl:apply-templates select="//a[@attr1=2] | //a[not(@*)]"/>


The transformation yields the following result:

<a attr1="2" attr2="3">

2.1. Precedence

Precedence order (from highest precedence to lowest) between Boolean and comparison operators is shown in the following table.

Precedence Operators Description
1 ( ) Grouping
2 [ ] Filters
3 /


Path operations
4 &lt;




5 =


6 | Union
7 not() Boolean not
8 and Boolean and
9 or Boolean or

3. Conclusion

In this example we learned about the XPath Union operator. We discussed what it is and how it works. We saw the working of the union operator with the help of an example as well. In the later section we also looked into the precedence order between boolean and comparison operators.

Mohammad Meraj Zia

Senior Java Developer
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button