Microsoft XML Core Services (MSXML) 4.0 - XPath Reference

Set Operations

XML Path Language (XPath) supports the set operation |.

"|" Operator

The "|", or union, operator returns the union of two queries; that is, it returns the set of nodes returned for one query combined with the set of nodes that satisfy the other query. Multiple union operators can be combined to union together the results of multiple queries. The union operator preserves document order and does not return duplicates.

The Microsoft implementation extends the union operator to be available anywhere in a query, not just at the top level. To support this behavior, the following constraint is applied to unions: All queries combined with the union operator must be from the same subtree and have the same root.

The following table demonstrates examples of valid and invalid queries based on these constraints:

Valid use of | (same subtree) Invalid use of | (different subtrees)
a | b | c a | /b | c
/a | /b | /c a | /b | c
a | . a | id('C1')
a | b a | ../b
a | b a | current()/b
id(x | y) id(x) | id(y)

Examples

Find all first-names and last-names:

first-name | last-name

Find all books and magazines from a bookstore:

bookstore/(book | magazine)

Find all books and all authors:

book | book/author

Find the first-names, last-names, or degrees from authors within either books or magazines:

(book | magazine)/author/(first-name | last-name | degree)

Find all books with author/first-name equal to "Bob" and all magazines with price less than 10:

book[author/first-name = "Bob"] | magazine[price < 10]

The following will return the first node that matches, not multiple nodes:

id(x | y)

Precedence

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

1 ( ) Grouping
2 [ ] Filters
3 / // Path operations
4 < <= > >= Comparisons
5 = != Comparisons
6 | Union
7 not() Boolean not
8 and Boolean and
9 or Boolean or

Example

XML File (books.xml)

Use the Sample XML File for XPath Syntax (inventory.xml) and change the href attribute to reference xpsetops.xsl.

XSLT File (xpsetops.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- suppress text nodes not covered in subsequent template rule -->
<xsl:template match="text()"/>

<xsl:template match="//book">
   <p/>first-name | last-name -- 
      <xsl:value-of select='first-name | last-name'/>
   <p/>bookstore/(book | magazine) -- 
      <xsl:value-of select='bookstore/(book | magazine)'/>
   <p/>book | book/author -- 
      <xsl:value-of select='book | book/author'/>
   <p/>(book | magazine)/author/(first-name | last-name | degree) -- 
      <xsl:value-of select='(book | magazine)/author/(first-name | last-name | degree)'/>
   <p/>book[author/first-name = "Bob"] | magazine[price &lt; 10] -- 
      <xsl:value-of select='book[author/first-name = "Bob"] | magazine[price &lt; 10]'/>
   <p/>id(x | y) -- 
      <xsl:value-of select='id(x | y)'/>
</xsl:template>

</xsl:stylesheet>

Formatted Output

This example doesn't generate significant output. It simply demonstrates that no errors are generated by the above XPath expressions.

See Also

Sample XML File for XPath Syntax (inventory.xml) | XPath Examples