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

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

Another situation where these rules matter is if one branch contains a call on an external function that has side effects. Writing such functions is something that's been left very much implementation-defined, but many XPath implementations will allow calls to external routines, and once they allow that, it's impossible to prevent such functions having arbitrary side effects. If you want to prevent a subexpression being evaluated because it has side effects, the only reliable way to ensure this is with an
if
expression; don't rely on
and
and
or
.

Note that there are no null values in XPath, as there are for example in SQL, and there is therefore no need for three-valued logic to handle unknown or absent data. Instead, you may need to test explicitly for absent values, as shown in some of the examples below.

Examples

Expression
Description
$x>3 and $x<8
Returns true if the value of variable
$x
is greater than 3 and less than 8.
@name and @address
Returns true if the context node has both a
name
and an
address
attribute. (Both the operands are sequences of nodes, which are converted to the
xs:boolean
true
if they contain at least one node, and to
false
if they are empty).
string(@name) and string(@address)
Returns true if the context node has both a
name
and an
address
attribute and if neither is a zero-length string. (Both the operands are strings, which are converted to the
xs:boolean
true
if their length is non-zero. If an attribute is absent, the sequence will be empty, and its string value will therefore be the empty string).
true()
A trivial
AndExpr
consisting of a single function call.
$x=5 or $x=10
Returns true if the variable
$x
has the value 5 or 10. This could also be written as
$x
=
(5,
10)
.
@name or @id
Returns true if the context node has a
name
attribute, an
id
attribute, or both.
not(@id) or @id=“”
Returns true if the context node has no
id
attribute or if it has an
id
attribute and the value is an empty string.
//para[position()=1 or position()=last()]
Selects the

elements that are either the first or the last (or the only)

children of their parent node.

Summary

This chapter described the following groups of XPath operators:

  • Arithmetic operators,
    +
    ,
    -
    ,
    *
    ,
    div
    , and
    mod
  • Value comparison operators
    eq
    ,
    ne
    ,
    lt
    ,
    le
    ,
    gt
    ,
    ge

Other books

Only the Hunted Run by Neely Tucker
3 Strange Bedfellows by Matt Witten
Three-Point Play by Todd Hafer
When Love Takes Over You by Norah C. Peters
The Perfect Kiss by Amanda Stevens