how do i add a comment to an xpath?
Asked Answered
C

4

12

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}
Cabdriver answered 4/11, 2009 at 21:4 Comment(2)
Where do you have the XPath? What language are you using it from? What is that language's comment syntax?Thromboembolism
i mean is it possible to add comments naively to an xpath.Cabdriver
C
16

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.

Cristionna answered 4/11, 2009 at 22:58 Comment(1)
Logged in just to upvote this ancient answer. Genius!Bit
H
24

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

Hairsplitter answered 25/3, 2014 at 23:35 Comment(1)
This should be the marked answer. Works just fine with Saxon HE 9.6.Chump
C
16

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.

Cristionna answered 4/11, 2009 at 22:58 Comment(1)
Logged in just to upvote this ancient answer. Genius!Bit
P
7

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

Phenomenology answered 4/11, 2009 at 21:10 Comment(1)
The XPath 1.0 specification does not allow comments. However, XPath 2.0 does allow comments (: this will be a comment in XPath 2.0 :)Loriannlorianna
H
0
  • Use   (: - for opening comment
             :) - for closing comment

  • Example

    (: //a[*] :)

  • Before using comment

enter image description here

  • After using comment (: :)

enter image description here

Hash answered 2/4 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.