Solved

What's the XPath for this example?

Posted on 2013-06-06
5
259 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
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML and CSS for a complex page 20 61
HTML File in SharePoint 2013 Library 4 47
Help position a graphic properly via CSS 4 23
Full Screen problem when auto scale IFRAME 2 65
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now