XML technologies / XPath / XPath 2.0: language reference / XPath expressions: operators and keywords / XPath 2.0 comments

(Excerpt from " XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 4.)

Comment delimiters: (:, :)

XPath 2.0 also extends XPath 1.0 by allowing a comment within an XPath expression. Complex expressions can thus include explanations. XPath ignores sections enclosed in comment delimiters, so their presence does not affect the result. Comment contents are normally not exposed to applications and serve only human readers.

Comment syntax:

(: ich bin ein XPath 2.0-Kommentar :)

The character sequences (: and :) serve as delimiters. Delimiters are not forbidden inside comments, so unlike most languages, XPath allows nested comments. Inner delimiters must occur in matched pairs, completing each nesting level:

(: das ist  (: eine erlaubte :)  Verschachtelung :)

(: das hier ist  (:  nicht erlaubt, weil unvollständig :)

Comments are permitted only where ignorable whitespace (ignorable) is allowed.

Looking back at XPath 1.0

XPath 1.0 has no comments, so explanations cannot be inserted within its expressions.

A look at XQuery

XQuery, not XPath itself, has similar syntax (:: and ::)marking pragmas and must-understand extensions.

The sequences (:: and ::) should therefore be avoided in XPath comments, and their content should not begin with a colon as a precaution.

(:::Dieser XPath-Kommentar gleicht einem XQuery-Pragma:::)

Colons are allowed in comments, including at the start. As a precaution, however, put a space after the opening delimiter and before the closing one, although the grammar does not explicitly require this:

(:  :::So ist ein XPath-Kommentar sicherer:::  :)

Pragmas can provide an XQuery application with processing information if supported. A pragma must be identified by a QName; its introductory keyword is pragma. The value follows the QName, separated by a space:

(:: pragma mein:timeout 1000 ::)

This fictional pragma limits query processing to 1000 ms, after which a timeout is reported. An application that does not recognize the requested feature ignores the pragma.

Must-understand extensions are proprietary features that the processing XQuery application must recognize (must-understand) because correct execution depends on them. They are declared with

(:: extension mein:xquery-supererweiterung ::)

After keyword extension comes the QName of the extension that must be supported. An application lacking this support reports a static error.

Pragmas and extensions are not currently part of XPath itself. If such an instruction appears within an XPath expression used outside XQuery, it will probably, but not certainly, be treated as a comment.

   

back to top