?
Solved

What's the XPath for this example?

Posted on 2013-06-06
5
Medium Priority
?
264 Views
Last Modified: 2013-06-08
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
Comment
Question by:lucavilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
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

by:lucavilla
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

by:Geert Bormans
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

by:lucavilla
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

by:Geert Bormans
ID: 39232174
welcome
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question