XPath: How to select node with some attribute by index?
I have several nodes开发者_开发技巧 with some particular attribute and I need to select one of them by index. For example I need to select second <div>
with 'test' class - //div[@class='test'][2]
doesn't work.
Is there a way to select node with some attribute by index ? How to do it?
This is a FAQ.
In XPath the []
operator has a higher precedence (binds stronger) than the //
pseudo-operator.
Because of this, the expression:
//div[@class='test'][2]
selects all div
elements whose class
attribute is "test" and who (the div
elements) are the second such div
child of their parent. This is not what you want.
Use:
(//div[@class='test'])[2]
I believe per XML specification, attributes are not considered to have an order.
Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.
See here
I think you'd be best of re-factoring your structure such that attribute order does not describe anything. If you can give any more details we might be able to offer suggestions.
EDIT: Re-reading your post, looks like you are trying to find node order and not attribute order. Node order is allowed and your syntax looks OK off-hand. What software are you doing this in?
精彩评论