Select Nth child Node in HTML Document

c#, html, html-agility-pack, xpath

Solution

Use:

(//table[count(ancestor::table) = 4])[1]

This selects the first `table` in the document that has exactly four ancestors named `table` .

Problem

I have an html document that I need to grab all `table` elements that are the 5th table deep in the `DOM`, not to be confused with the 5th child `table`. My problem is this 5 `table` deep structure could be wrapped in any number of `div` elements so I can't use an absolute path such as /html/body/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table For example: ``` <body> <table> <table> <table> <table> <!--Grab this one --> <table> </table> </table> </table> </table> </table> </body> ``` Or This: ``` <body> <div> <!--Could be wrapped more than just once though --> <table> <table> <table> <table> <!--Grab this one --> <table> </table> </table> </table> </table> </table> </div> </body> ```

Original source