Michael Shalmi
Michael Shalmi

Reputation: 35

How to grab Index instead of relative position using xpath

Given the following xml:

<randomName>
    <otherName>
        <a>item1</a>
        <a>item2</a>
        <a>item3</a>
    </otherName>
    <lastName>
        <a>item4</a>
        <a>item5</a>
    </lastName>
</randomName>

Running: '//a' Gives me an array of all 5 "a" elements, however '//a[1]' does not give me the first of those five elements (item1). It instead gives me an array containing (item1 and item 4).

I believe this is because they are both position 1 relatively. How can I grab any a element by its overall index?

I would like to be able to use a variable "x" to get itemX.

Upvotes: 1

Views: 69

Answers (1)

Keith Hall
Keith Hall

Reputation: 16095

You can wrap it in parenthesis so it knows to apply the index to the entire result set

(//a)[1]

Upvotes: 2

Related Questions