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
5.36Mb 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

The Beggar's Opera by Peggy Blair
The Borgia Dagger by Franklin W. Dixon
Shadow Man: A Novel by Jeffrey Fleishman
When Girlfriends Step Up by Page, Savannah
Knives and Sheaths by Nalini Singh
A Dog's Purpose by W. Bruce Cameron
The Last Temptation by Val McDermid
The Other Child by Charlotte Link
Beautifully Unnatural: A Young Adult Paranormal Boxed Set by Amy Miles, Susan Hatler, Veronica Blade, Ciara Knight
A Lady of Letters by Pickens, Andrea