priyank
priyank

Reputation: 4724

XPath to find elements with attributes

How can I find elements that has at least one attribute?

Example:

<tr>...</tr>
<tr style="">...</tr>
<tr width="">...</tr>

I want all tr elements but ...

I tried following xpath but it doesn't work.

//table//tr[contains(attributes::*,'')]

Thanks

Upvotes: 1

Views: 1144

Answers (1)

Welbog
Welbog

Reputation: 60378

This should do it:

//table/tr[@*]

The reason why yours doesn't work is because contains() will always return true when the second parameter is ''. When an expression returns a node set within square brackets, it is considered true if it's non-empty, false if it's empty. So [@*] will return the set of all attributes and will be interpreted as true if there are any present.

Upvotes: 3

Related Questions