开发者

xpath expression for when node doesn't exist?

I am dealing with some irregular开发者_JAVA百科ities in my documents, mainly, certain nodes are present and some other times, it's not there. I want an xpath which is able to take account both situations.

For example across my documents, there are cases when span is present and when it's not there.

/html/body/span/div/table/tr/td/a
/html/body/div/table/tr/td/a

How can I express an xpath which will match both cases above ?


The // selector will search arbitrarily deep. This will work with or without the span (it will also find divs inside of other tags besides body and span):

/html/body//div/table/tr/td/a

You can join two result-sets together using the union | operator:

/html/body/span/div/table/tr/td/a |
/html/body/div/table/tr/td/a

You could use use some crazy filtering to try to do it all in one path. Untested:

/html/body/descendant-or-self::*[self::body or self::span/parent::body]/div/table/tr/td/a
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜