Pyt
Pyt

Reputation: 1935

XPath: how to select nodes by IN condition?

Is it possible to select nodes in a similar way?

'./tr[position() in (1, 3, 7)]'

I found only this solution:

'./tr[position() = 1 or position() = 3 or position() = 7]'

Upvotes: 1

Views: 181

Answers (1)

Tobias Klevenz
Tobias Klevenz

Reputation: 1645

In XPath 2.0 you would simply do:

./tr[position = (1,3,7)]

In XPath 1.0 the usual way to do it is the solution you already found, an alternative that is a bit shorter would be something like:

./tr[contains('1 3 7', position())] 

The spaces in the string are essential here, otherwise you'd also get nodes 13,37 and 137.

Upvotes: 4

Related Questions