Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7916793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:56:01+00:00 2026-06-03T14:56:01+00:00

There’s probably a better way to do this than what I’d doing, because I’m

  • 0

There’s probably a better way to do this than what I’d doing, because
I’m stuck in a metaphorical pothole.

I want to get some of the nodes beneath a particular node. I came up
with this XPath expression:

>>> content_tags = 'h1 h2 h3 h4 h5 h6 p ol ul dl table'.split() 
>>> content_xpath = './/*[%s]' % ' or '.join('self::%s' % i for i in content_tags) 
>>> content_xpath 
'.//*[self::h1 or self::h2 or self::h3 or self::h4 or self::h5 or 
self::h6 or self::p or self::ol or self::ul or self::dl or 
self::table]' 

Any of the listed content_tags can be the top of the hierarchy I’d
wanting, and I want to ignore other elements that may be at the same
or higher levels. Unfortunately, sometimes there’s a <p> inside a
<ul> or a <table>, or a <table> inside a <ol>, etc., and I get the
inner element as a separate result along with the outer. Is there a good way to
perform a “cut” to ignore nodes that may be nested inside one that
I’ve found? Or is there some better way of doing this that I’m
somehow missing?

Here’s an example of what I’m trying to parse.

<div class="interesting"> 
<img src="ignore-this.jpg"/> 
<h1>I want this.</h1> 
<p>I want this, too.</p> 
<div class="sidebar"> 
<ul> 
<li><p>I only want one copy of this, inside the UL.</p></li> 
<li><p>Ditto.</p></li> 
</ul> 
</div> 
</div> 

Thanks!

BTW, I found a few posts on a w3.org mailing list that advocated a
“dont-include- any-descendant-or-self” filter, which I think would do
exactly what I want, but it doesn’t seem to have made it into the
final spec. 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:56:02+00:00Added an answer on June 3, 2026 at 2:56 pm

    Searching as with //p is explicitly recursive — if that’s not what you want, don’t do that! 🙂

    If you only want a p that’s directly under an interesting div, but that div can be anywhere in your hierarchy, this would be expressed as such:

    //div[@class='interesting']/p
    

    …if you only want a p that’s directly under the location in your tree the search is relative to, that’s even simpler:

    ./p
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is probably an easy answer for this, but when I added DateTime.Now.ToString() to
There is previous little on the google on this subject other than people asking
There is this SQL Statement SELECT t1.Name ,Count(t2.SubID) Totals -- I don't know how
For some reason, after submitting a string like this Jack’s Spindle from a text
There is this strange situation I'm fighting on. I have 3 pages, les call
There are a lot of Jquery plugins and libraries. and sometimes I want to
There must be a simple solution to this, but after 4 hours of browsing
There are things like f.call(...) f.apply(...) But then there's this (1, alert)('Zomg what is
There are a few ways to get class-like behavior in javascript, the most common
There's a common error that gets thrown by the Visual C Runtime: This application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.