tags:

views:

33

answers:

1

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

+3  A: 

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.

Welbog
No, it returns all rows. I believe all strings will contain ''. That's why it is returning all tr nodes.
priyank
@priyank: I just gave it a test in Visual Studio's XSLT interpreter and my example works as expected.
Welbog
@priyank: It surely isn't the same. Use XPather and inspect this page, using `//div` and `//div[@*]` - they yield different number of items.
ANeves
@welbog Your example works perfect. I was talking about the XPath I wrote earlier. /tr[contains(attributes::*,'')]Thx.
priyank