Andrej
Andrej

Reputation: 736

xpath get specific child

let's say I have a html structure like this -> (note: in my code there are no IDs, i just put them here so it's easier to explain)

  <body>
    <table id=a>
      <tr>
        <td>
          <table id=b>
            <tr>
              <td>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    <table id=c>
      <tr>
        <td>
        </td>
      </tr>
    </table>
  </body>

Using XPath, //body/table[1] gives me the inner table with the id "b", but what I really want is it's first child ("c"). Something along th lines of $("body >table").eq(1) but i'm using c#. How do I do that using XPath?

Tnx!

EDIT: it is not an option me to select the [2] element, since this is only a simplified explanation of the problem i'm heaving...

Andrej

Upvotes: 0

Views: 1520

Answers (2)

Prashanth Thurairatnam
Prashanth Thurairatnam

Reputation: 4361

The element selectors starts from index 1 (and not from 0). Hence you will have to use the xpath query //body/table[2]

Here is the explanation with screen shots.

For 0 based index node will be null;

enter image description here

But since the index starts with 1, for indeces 1 and 2 these nodes will be returned

enter image description here

enter image description here

Still not convinced? Let's check for the inner table element.

First let's check with 0 based index (again it is going to be null)

enter image description here

However when the index is 1 it will return the correct node

enter image description here

Upvotes: 1

fenix2222
fenix2222

Reputation: 4730

Try

//body/table[not(ancestor::td)]

Upvotes: 0

Related Questions