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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
12.73Mb size Format: txt, pdf, ePub
  • As the top-level XPath expression
  • Within a parenthesized expression
  • Within the parentheses of an
    if
    expression
  • Within square brackets as a predicate

Neither of the last two is remotely useful, so in practice the rule is: if you want to use the comma operator to construct a list, then either it must be at the outermost level of the XPath expression or it must be written in parentheses.

For example, the
max()
function expects a single argument, which is a sequence. If you want to find the maximum of three values
$a
,
$b
, and
$c
, you can write:

max(($a, $b, $c))

The outer parentheses are part of the function call syntax; the inner parentheses are needed because the expression
max($a, $b, $c)
would be a function call with three parameters rather than one, which would be an error.

XPath does not use the JavaScript convention whereby a function call with three separate parameters is the same as a function call whose single parameter is a sequence containing three items.

The operands of the
,
operator can be any two sequences. Of course, a single item is itself a sequence, so the operands can also be single items. Either of the sequences can be empty, in which case the result of the expression is the value of the other operand.

The comma operator is often used to construct a list, as in:

if ($status = (‘current’, ‘pending’, ‘deleted’, ‘closed’)) then …

which tests whether the variable
$status
has one of the given four values (recall from Chapter 8 that the
=
operator compares each item in the sequence on the left with each item in the sequence on the right, and returns true if any of these pairs match). In this construct, you probably aren't thinking of
,
as being a binary operator that combines two operands to produce a result, but that's technically what it is. The expression
A,B,C,D
technically means
(((A,B),C),D)
, but because list concatenation is associative, you don't need to think of it this way.

Other books

Fortress Rabaul by Bruce Gamble
Chained By Fear: 2 by Melvin, Jim
Demands of Honor by Kevin Ryan
Don't Even Think About It by George Marshall
Fear the Dead (Book 4) by Lewis, Jack
Us and Uncle Fraud by Lois Lowry
Absolutely Captivated by Grayson, Kristine
Homicide My Own by Anne Argula