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

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
4.55Mb size Format: txt, pdf, ePub
Expression
Description
$x + ($y * 2)
Returns the result of multiplying
$y
by two and adding the value of
$x
.
//book | //magazine
Returns a sequence of nodes containing all of the

and

elements in the same document as the context node. (This could also be written, perhaps more efficiently, as
//(book|magazine)
.)
substring-before(author, ‘ ’)
Finds the value of the first

child of the context node and returns that part of the value that precedes the first space character.
chapter and verse
Returns the
xs:boolean
value
true
if the context node has a child

element and also a child

element.
93.7
Returns the decimal value
93.7
.
sum(//product/(price * qty))
Returns the result of multiplying the values of

and

for every

element in the document, and summing the results.
avg((//product)[position() le 5])/price)
Returns the average

of the first five

elements in the document.

Lexical Constructs

An XPath 2.0 expression is written as a sequence of Unicode characters. Every character that's available in XML 1.0 can be used in an XPath expression, and possibly characters that are available in XML 1.1 as well, though that's been left up to the implementation to decide.

XPath itself isn't concerned with how these characters are encoded. XPath expressions will often be embedded in other languages such as XSLT, or they may be constructed as runtime character strings using a programming language such as Java or JavaScript. Any escape conventions local to the host language will be applied before the XPath parser gets to see the expression, and the syntax described in the XPath Recommendation (and in the XPath chapters of this book) is the syntax after such escapes have been expanded. For example:

  • When XPath expressions are written in an XSLT stylesheet, the escaping conventions of XML apply. This means, for example, that a
    <
    character must be escaped as
    <
    and an ampersand as
    &
    . Since XPath expressions are invariably written inside an attribute value in the stylesheet, the delimiting quotation marks of the attribute value (usually

    , but you can choose
    '
    if you prefer) must also be escaped, typically as
    "
    or
    '
    , respectively. It's also worth remembering that the XML parser normalizes whitespace in an attribute value, so if you want to write an expression that tests whether some element in your source document contains a tab character, you should write this as

Other books

Essence of Time by Liz Crowe
New Year Island by Draker, Paul
Holding the Zero by Seymour, Gerald
The Forbidden by William W. Johnstone
Tongues of Fire by Peter Abrahams
Vampire Academy by Richelle Mead
The Silent Hour by Elisabeth Grace Foley