Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i add a comment to an xpath?

Tags:

xpath

for example i have an xpath and wish to add a comment near it to identify it.

/html/body/div/table/tr/td/a{this is a link}
like image 716
gweg Avatar asked Nov 04 '09 21:11

gweg


3 Answers

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 --}

like image 188
Sepster Avatar answered Nov 17 '22 08:11

Sepster


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.

like image 42
andre-r Avatar answered Nov 17 '22 08:11

andre-r


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...)

like image 42
mjv Avatar answered Nov 17 '22 08:11

mjv