0

I am trying to grab the elements of index 3 and 4 in the following xml:

<Automated>
    <Group>
        <Test><id>testId</id>...</Test>
        <Test>...</Test>
        <Test>...</Test> <!-- 3? -->
    </Group>
    <Test>...</Test> <!-- 4? -->
</Automated>

As far as I was aware, the expression //x was used to grab all elements with type x. The expression I was attempting to use to grab the 3rd and 4th elements is:

//Test[3] , //Test[4]

However, element //Test[4] does not return anything. Upon further investigating, I've realized that //Test[1] will actually return both elements 1 and 4. Which is actually the first child of the first element, and the second (first test?) child.

Is there any way to achieve what I want to do?

The only other thing that I can think of to do (since I'm using this in c# and have access to scripting this) is using a counter to iterate through all possible selections of //Test[x] and then index it myself. However, that seems like more work than is necessary.

Polosky
  • 88
  • 5
  • 13

1 Answers1

0

As it turns out, one can use parentheses to fix this issue, as xPath will use those for precedence.

(//Test)[4]

will give me the fourth element of all Test elements in the xml document, whereas

//Test[4]

does not exist in the document, as there is no element with 4 total Test children.

Polosky
  • 88
  • 5
  • 13