Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# What's the XPath for this example?

Posted on 2013-06-06
Medium Priority
268 Views
What's the XPath for grabbing a "<DIV id="myid">...</div>" piece of an html page and only if there are 2 to 5 nested DIVs?
0
Question by:lucavilla
• 3
• 2

LVL 60

Accepted Solution

Geert Bormans earned 2000 total points
ID: 39225361
//DIV[@id = 'myid'][count(ancestor::DIV) >= 2 and count(ancestor::DIV) <= 5 ]
if you mean nested inside 2 to 5 div

//DIV[@id = 'myid'][count(descendant::DIV) >= 2 and count(descendant::DIV) <= 5 ]
if you mean to have nested inside 2 to 5 div

//DIV[@id = 'myid'][(count(ancestor::DIV) + count(descendant::DIV)) >= 2 and ][(count(ancestor::DIV) + count(descendant::DIV))  <= 5 ]
if you need it to be inside a sequence of 2 to five div
0

Author Comment

ID: 39229017
Great Gertone!

Only a problem because I was not clear in my question.  How can I adapt your second solution to count only the *levels* of sub-DIVs?

Example:
<div>abc
<div>def
</div>ghi
<div>lmn
</div>opq
</div>

Your solution counts 2 descendant DIVs.
How can I tell you count only the sub-levels of DIVs, i.e. 1?
0

LVL 60

Expert Comment

ID: 39231738
mmh, then it gets tricky

you could test for
//DIV[@id = 'myid'][DIV/DIV| DIV/DIV/DIV| DIV/DIV/DIV/DIV| DIV/DIV/DIV/DIV/DIV ]
bit clumsy, I know, I hope you don't have to do this for 2 to 40 :-)

the other way is to go as deep as possible and look up (that is the only ay to cound a single path), but that only works as a replaement for the third situation
0

Author Comment

ID: 39232089
Wow thanks it works, you're a genius!  and thanks to you I'm starting to love this (still unknown to me) XPATH, I start to read a tutorial to learn it!
0

LVL 60

Expert Comment

ID: 39232174
welcome
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.