XPath

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:

a.xml

<?xml version="1.0"?>
<example>
  <a attr1="1">
    <a attr1="2" attr2="3">
      <a>
        <b>Hello</b>
        <b>World!</b>
      </a>
    </a>
  </a>
</example>

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.

a.xsl

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <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="."/>
      </xsl:if>
    </xsl:element>
  </xsl:template>

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

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

</xsl:stylesheet>

The transformation yields the following result:

<a attr1="2" attr2="3">
  <a>
    <b>Hello</b>
    <b>World!</b>
  </a>
</a>
<a>
  <b>Hello</b>
  <b>World!</b>
</a>

2.1. Precedence

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

PrecedenceOperatorsDescription
1( )Grouping
2[ ]Filters
3/

//

Path operations
4&lt;

&lt;=

&gt;

&gt;=

Comparisons
5=

!=

Comparisons
6|Union
7not()Boolean not
8andBoolean and
9orBoolean 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
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button