开发者

how do i add a comment to an xpath?

开发者 https://www.devze.com 2022-12-10 18:32 出处:网络
for example i have an xpath and wish to add a comment near it to id开发者_如何学运维entify it.

for example i have an xpath and wish to add a comment near it to id开发者_如何学运维entify it.

/html/body/div/table/tr/td/a{this is a link}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号