Select Nth child Node in HTML Document

c# html html-agility-pack xpath

Question

I have to get every element from an HTML page.table components that are buried in the fifth tableDOM to distinguish it from the fifth childtable . My issue is this:table Numerous materials might be used to encase deep structures.div I am unable to utilize an absolute path like

/html/body/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table

For instance:

<body>    
    <table>    
        <table>
            <table>
                <table>
                   <!--Grab this one -->
                   <table>
                   </table>
                </table>
            </table>
       </table>
    </table>
</body>

Also 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>
1
3
11/21/2012 7:58:18 PM

Accepted Answer

Use:

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

This chooses the initialtable in the text when there are four ancestors listed preciselytable .

3
11/21/2012 8:37:31 PM

Popular Answer

I think you'd like the// between each component, completing the whole expression:

//table//table//table//table//table

Any table with 4 tables in it will be chosen by this.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow