how do i add a comment to an xpath?

XPATH 2.0 does allow comments.

From http://www.w3.org/TR/xpath20/#comments:

Comments may be used to provide informative annotation for an expression. Comments are lexical constructs only, and do not affect expression processing.

Comments are strings, delimited by the symbols (: and :). Comments may be nested.

A comment may be used anywhere ignorable whitespace is allowed (see A.2.4.1 Default Whitespace Handling).

The following is an example of a comment:

(: Houston, we have a problem :)

Bad news if we ever need to parse XML containing emoticons! :-)

As an aside - as I was looking for this info in the context of working with Tibco Designer for BusinessWorks v5.x, where comments can be added within the TIBCO Designer XPATH formula builder using:

{-- Houston, we've had a problem --}


Not a comment syntax, but you can give string literals as predicate, which evaluates as true (imho) and should not change the outcome of the expression. I don't know if this has big performance drawbacks.

/html/body/div/table["this is"]["a table"]/tr/td/a["this is a link"]

But like mjv said, I also would stick to the syntax of the host language.


2019 edit
As pointed out in @Sepster's reply and elsewhere, starting with XPath 2.0, comments became possible with their cute "smiley face"-looking syntax. I'm only about 10 years late in editing this reply to mention very useful fact ;-)

Original reply c. 2009 (assumed XPATH 1.0)
No, the XPATH syntax doesn't allow to embed comments within the path string.

This is typically not a significant limitation because paths are usually short and a comment can be placed nearby, in the particular syntax of the host language (XSLT, C#, whatever...)

Tags:

Xpath