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

  • Home
  • SEARCH
  • 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 501197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:09:54+00:00 2026-05-13T06:09:54+00:00

I’m trying to get an XPath expression together that will give me all the

  • 0

I’m trying to get an XPath expression together that will give me all the descendent elements of a node that match a filter (e.g. [contains(@class,”interesting”)] but which don’t have a specific ancestor e.g. [contains(@class,”frame”)]. Probably best explained by example:

    <div class="frame">
        <p class="interesting">alice</p>
        <p class="interesting">bob</p>
        <p class="interesting">carol>/p>

        <div> 
            <div>
                <h3 class="interesting">david</h3>
            </div>
        </div>

        <div class="frame">
            <p class="interesting">drevil</p>
        </div>
    </div>

So in this example, I want to be able to match all the “interesting” elements, that are descendents of the first div with class=”frame”. But I don’t want the “interesting” elements underneath the nested “frame” div.

Ideally I’d have a single XPath expression that would give me those elements with content alice, bob, carol and david. But not drevil.

It is like the presence of the nested frame occludes that branch of the tree from the search.

Any ideas? All responses much appreciated.


In response to Robert, I have this Python code (though I will utlimately do it browser side):

from lxml import etree

from StringIO import StringIO

testxml = """
<div>
    <div class="frame">
        <p class="interesting">alice</p>
        <p class="interesting">bob</p>
        <p class="interesting">carol</p>

        <div> 
            <div>
                <h3 class="interesting">david</h3>
            </div>
        </div>

        <div class="frame">
            <p class="interesting">drevil</p>
        </div>
    </div>    
</div>
"""

xsl = """
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <output>
           <xsl:apply-templates select="//div[@class='frame'][1]/*"/>
        </output>
    </xsl:template>

    <xsl:template match="*">
       <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="*[@class='frame']"/>

    <xsl:template match="*[@class='interesting']">
       <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>
"""


def test_xsl():
    xslt_doc = etree.parse(StringIO(xsl))
    transform = etree.XSLT(xslt_doc)
    doc = etree.parse(StringIO(testxml))
    result = transform(doc)
    print result

if __name__=="__main__":
    test_xsl()

This gives the following result:

<?xml version="1.0"?>
<output>
    <p class="interesting">alice</p>
    <p class="interesting">bob</p>
    <p class="interesting">carol</p>
    <h3 class="interesting">david</h3>
    <p class="interesting">drevil</p>
</output>

As you can see drevil is lurking.

Note, Tomalak is correct in that the 2nd match on * has no effect (other than to remove spaces from the output which is a bit odd!).

It just twigged though that I might not be able to go with the XSLT approach, the whole point of doing an XPath query in the first place was to gain references to nodes within the original HTML document. If I do a transform, the nodes contained in the new result document will be copies and not the original ones I’m looking for and thus no use!

This might be the dumbest question ever, but is there a way to maintain a references from nodes in the transformed document to nodes in the original?

Thanks Tomalak, Robert and mykhal for your help so far. I think I just need to buy a book on XSLT…

  • 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-05-13T06:09:54+00:00Added an answer on May 13, 2026 at 6:09 am

    you can use selector limiting ancestor div[@class=”frame”] elements to 1

    //div[@class="frame"][1]//*[@class="interesting" and count(ancestor::div[@class="frame"])=1]
    

    it worked:

    >>> import lxml.html
    >>> data = """
            <div class="frame">
                <p class="interesting">alice</p>
                <p class="interesting">bob</p>
                <p class="interesting">carol</p>
    
                <div> 
                    <div>
                        <h3 class="interesting">david</h3>
                    </div>
                </div>
    
                <div class="frame">
                    <p class="interesting">drevil</p>
                </div>
            </div>
        """
    >>> tree = lxml.html.fromstring(data)
    >>> tree.xpath('//div[@class="frame"][1]//*[@class="interesting" and count(ancestor::div[@class="frame"])=1]/text()')
    ['alice', 'bob', 'carol', 'david']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.