[2.0] Set Operators—except, intersect, and union
One weakness of XPath 1.0 was the inability to compare sets of nodes. If we
selected two node-sets with two XPath 1.0 expressions, it was
difficult to tell which nodes were in both sets and which were in one
set but not the other. XPath 2.0 has two new set operators, except
and intersect
. If you enjoy extra typing, XPath
2.0 also adds the union
operator, a
synonym for the vertical bar operator (|
) in XPath 1.0. (The vertical bar is still
supported in XPath 2.0.)
Be aware that these operators work only on sequences of nodes. If you try to use them with sequences that contain atomic values, you’ll get an error.
As we discuss these operators, we’ll use this XML document:
<?xml version="1.0" encoding="utf-8"?>
<!-- books.xml -->
<favorite-books>
<booklist>
<book isbn="0596000537"
favorite="Doug Sheri">XSLT</book>
<book isbn="0141439777"
favorite="Doug">Tristram Shandy</book>
<book isbn="0142437298"
favorite="Doug">Herzog</book>
<book isbn="0679762108"
favorite="Doug Sheri">The Sportswriter</book>
<book isbn="0143035479"
favorite="Sheri">The Girls' Guide to Hunting and Fishing</book>
<book isbn="0375724443"
favorite="Sheri">Ava's Man</book>
</booklist>
</favorite-books>
We’ll select two sequences in our sample stylesheets: the books
I like (the favorite
attribute
contains the string Doug
) and the
books my wife likes (the favorite
attribute contains the string Sheri
). We’ll use those two sequences to
illustrate the set operators.
Note
The except ...
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.