softwarevamp
softwarevamp

Reputation: 857

Xpath combine predicates with common ancestor?

I want immediate tr of table optionally wrapped in tbody:

//table[complex-predictor]/tbody/tr | //table[complex-predictor]/tr

I want to combine the predicates as:

//table[complex-predictor](/tbody/tr | /tr)

But it not works. What is the correct way to do this?

Btw, i don't want tr deep in table (/tbody/tr/td/table/tbody/tr)

Upvotes: 2

Views: 78

Answers (1)

har07
har07

Reputation: 89305

This is one possible way :

//table//*[self::th|self::tr]

The main XPath returns all descendant elements of table, then the predicate (the expression in []) filters the descendants to be returned to only th and tr elements.

"Btw, i don't want tr deep in table (/tbody/tr/td/table/tbody/tr)"

In XPath 2.0 or above you can do :

//table[complex-predictor]/(tbody/tr|tr)

But in XPath 1.0, I don't see a clean way to get this done without repeating the 'complex-predictor'

Upvotes: 2

Related Questions