XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (428 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.93Mb size Format: txt, pdf, ePub

if ($N intersect $A/descendant::node()) then …

This works because if
$N
is among the descendants of
$A
, the intersection will contain
$N
, and the effective boolean value of a sequence containing one node is
true
. If
$N
is not among the descendants of
$A
, the intersection will be empty, and the effective boolean value of an empty sequence is
false
.

The
except
operator is useful when there is a need to process all the child elements of a node, or all its attributes, except for certain specific exclusions. For example, the XSLT instruction:


copies all the attributes of the context node to the result document except for the
last-changed
attribute (if there is one).

Set Intersection and Difference in XPath 1.0

XPath 1.0 provided no equivalent to the
intersect
and
except
operators. In XPath 1.0, if you want to form the intersection between two node-sets
$p
and
$q
, the following rather tortuous expression achieves it:

$p [ count( . | $q ) = count( $q ) ]

This selects the nodes in
$p
that are also in
$q
. They must be in
$q
, because their union with
$q
has the same number of nodes as
$q
itself.

Similarly, the following XPath 1.0 expression finds the nodes that are in
$p
and not in
$q:

$p [ count( . | $q ) != count( $q ) ]

If you see these constructs when you are upgrading XPath 1.0 code, you can confidently replace them with the XPath 2.0 constructs:

$p intersect $q

$p except $q

which are not only a lot easier to understand, but will probably be much more efficient as well.

Some XSLT 1.0 processors also provided extension functions to implement set intersection and difference; for example, the functions defined in the EXSLT library (
http://www.exslt.org
). These have been superseded by the new operators.

Sets of Atomic Values

The operators
union
,
intersect
, and
except
work only on sets of nodes. There are no equivalent operators in XPath 2.0 to handle sets of atomic values. However, it's easy to achieve the same effect for yourself, as follows:

Operator
Expression
Union
distinct-values($A, $B)
Intersect
distinct-values($A[. = $B])
Except
distinct-values($A[not(. = $B)])

Summary

XPath expressions are used to select data from the source document and to manipulate it to generate data to place in the result document. Path expressions play the same role for XML as the SQL
SELECT
statement plays for relational databases—they allow us to select specific parts of the document for transformation, so that we can achieve the required output.

This chapter has provided a full description of the meaning of path expressions, the
/
operator as it applies to nodes, steps, axes, node tests, and predicates, and it also covered the other operations defined on sequences of nodes, namely the
union
,
intersect
, and
except
operators.

The next chapter will describe constructs in the XPath language that operate on any kind of sequence, whether it contains nodes, atomic values, or a mixture of the two.

Chapter 10

XPath: Sequence Expressions

One of the most notable innovations in XPath 2.0 is the ability to construct and manipulate sequences. This chapter is devoted to an explanation of the constructs in the language that help achieve this.

Sequences can consist either of nodes, or of atomic values, or of a mixture of the two. Sequences containing nodes only are a generalization of the node-sets offered by XPath 1.0. In the previous chapter we looked at the XPath 2.0 operators for manipulating sets of nodes, in particular, path expressions, and the operators
union
,
intersect
, and
except
.

In this chapter we look at constructs that can manipulate any sequence, whether it contains nodes, atomic values, or both. Specifically, the chapter covers the following constructs:

  • Sequence concatenation operator
    :
    ,
  • Numeric range operator
    :
    to
  • Filter expressions
    :
    a[b]
  • Mapping expressions
    :
    for
  • Simple mapping expressions
    :
    /
    applied to atomic values
  • Quantified expressions
    :
    some
    and
    every

Other books

Fire in the Stars by Barbara Fradkin
Outbreak: Long Road Back by Van Dusen, Robert
False Impressions by Thayer, Terri
An Exchange of Hostages by Susan R. Matthews
Bridget Jones: Sobreviviré by Helen Fielding
Cuckoo (Kindred Book 3) by Scarlett Finn
Fleeced: A Regan Reilly Mystery by Carol Higgins Clark
Enough! (A Travesty and Ordo) by Donald E Westlake
Act of Will by Barbara Taylor Bradford