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 8667267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:55:30+00:00 2026-06-12T17:55:30+00:00

Having markup like this: <div class=foo> <div><span class=a1></span><a href=…>…</a></div> <div><span class=a2></span><a href=…>…</a></div> <div><span class=a1></span>some

  • 0

Having markup like this:

<div class="foo">
   <div><span class="a1"></span><a href="...">...</a></div>
   <div><span class="a2"></span><a href="...">...</a></div>
   <div><span class="a1"></span>some text</div>
   <div><span class="a3"></span>some text</div>
</div>

I am interested in getting all <a> and some text ONLY if adjacent span is of class a1. So at the end of the whole code my result should be <a> from first div and some text from third one. It’d be easy if <a> and some text were inside span or div would have class attribute, but no luck.

What I am doing now is look for span with a1 class:

//div[contains(@class,'foo')]/div/span[contains(@class,'a1')]

then I get its parent and do another query() with that parent as context node. This simply looks far from being efficient so the question clearly is if there is any better way to accomplish my goal?


THE ANSWER ADDENDUM

As per @MarcB accepted answer, the right query to use is:

//div[contains(@class,'foo')]/div/span[contains(@class,'a1')]/..

but for <a> it may be better to use:

//div[contains(@class,'foo')]/div/span[contains(@class,'a1')]/../a

the get the <a> instead of its container.

  • 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-12T17:55:31+00:00Added an answer on June 12, 2026 at 5:55 pm

    The nice thing about xpath queries is that you can essentially treat them like a file system path, so simply having

    //div[contains(@class,'foo')]/div/span[contains(@class,'a1')]/..
                                                                  ^^
    

    will find all your .a1 nodes that are below a .foo node, then move up one level to the a1 nodes’ parents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm having this markup: <div class=gui> <div class=form> <div class=textbox></div> <div class=textbox></div> <div class=textbox></div>
hi i'm having this markup: <div id=main> <div nam=item>1</div> <div nam=item>2</div> <div> <div nam=item>3</div>
i'm having this markup: <UL> <LI class=odd>bla</LI> <LI class=even>bla</LI> <LI class=odd>bla</LI> <LI class=even>bla</LI> <LI
This is my markup: <div class=contentSubBox> <h5>Please choose a report</h5> <div class=arrowNavigation> <div class=arrowNavigationLeft>
I have markup like this <div id=colorpicker-background> <div id=colorpicker-selector></div> </div> With JS like this:
Hi I have a div container having some string,Some child div have a text
i'm having the following markup using 2 divs. the #child div has set position:absolute;
In the following markup, "image 3" would the only visible div if all .image
I have this question on the Wordpress stack exchange as well, but not having
I'm having some trouble with a Link Button generating incorrect mark up. It should

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.