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

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

The following sections present the basic lexical constructs found within an XPath expression.

Comments

Comments may appear in an XPath expression anywhere that whitespace may appear. Comments begin with
(:
and end with
:)
, which feels slightly comical until you get used to it. But it means that comments are quite distinctive visually, and they read well because they look parenthetical.

Here is an example of a comment within an XPath expression:

if (string(@x))

then (: attribute x exists and is non-empty :) @x

else “none”

Was it really necessary for XPath to invent a completely new syntax for comments? Well, none of the obvious candidates would work. The C/Java convention is heavily reliant on symbols such as
/
and
*
, which are already overloaded in XPath. The SQL convention of
--
doesn't work because it's perfectly legal to have two adjacent hyphens in an XML name. The XML syntax of

doesn't work in an expression that's embedded in an XML attribute in a stylesheet. Because of XML attribute normalization, anything that attaches significance to line endings is ruled out. Curly braces were tried at one stage, but they are easily confused with the delimiters for attribute value templates in XSLT, or the equivalent embedded expressions in XQuery (and they were a new invention anyway). So smileys it is….

XPath 2.0 comments can be nested. This allows you to comment out a section of code even if it already contains comments. So, for example, the following expression is legal, and evaluates to 3:

3 (: + if (number(@x))

   then (: attribute x exists and is non-zero :) @x

   else 0

:)

To achieve this, the production rules for comments are given as follows:

Symbol
Lexical Rules
Comment
(:
(CommentContents | Comment)*
:)
CommentContents
Char

Other books

Hot Stuff by Don Bruns
We Are Pirates: A Novel by Daniel Handler
The Bloodlust by L. J. Smith
Blue Labyrinth by Douglas Preston, Lincoln Child
Cravings (Fierce Hearts) by Crandall, Lynn