当前位置: 动力学知识库 > 问答 > 编程问答 >

xpath expression for when node doesn't exist?

问题描述:

I am dealing with some irregularities 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
分享给朋友:
您可能感兴趣的文章:
随机阅读: