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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:33:06+00:00 2026-05-19T15:33:06+00:00

Hey, i’m trying to find out the parent-node of the nodes content text. example:

  • 0

Hey, i’m trying to find out the parent-node of the nodes content text.

example:

<div>
    <h1>Node to find</h1>
    <p>another node</p>
</div>

All my code know is what the text in the node is and my script needs to find out in what node the text contains.

i have tried the following xpaths:

 1. //*[. = "'. $text .'"]
 2. //*[contains(., "'. $text .'")]

the first gives me a empty nodeList
the second gives me a lot of nodes, but it gives me all the parent containing the text, i want only the first parent.

Thanks for any help.

  • 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-19T15:33:06+00:00Added an answer on May 19, 2026 at 3:33 pm

    I’m not sure I understand the "'. $text .'" part of your answer… I guess that means some sample text, not an intended reference to a variable named text?

    Anyway, when you use contains(., "foo") you are asking whether the current node’s string value contains “foo”. The current node’s string value is the concatenation of all descendant text nodes’ string values. That is why //*[contains(., "foo")] returns a list of nodes: it matches every ancestor of every text node containing “foo”. (And it can be very inefficient because you’re doing that concatenation function on every node in the tree.)

    The reason your starts-with() answer worked (sometimes) is that you got lucky: the parent node of the text node had other preceding siblings with their own text, so the grandparent node’s text value started with something else. Also very inefficient…

    If the text you’re looking for will only be in one text node — i.e. it will not be split up across multiple elements / comments / etc. — then you can efficiently and accurately match only the element containing the text node, using [edited]:

    //*[text()[contains(., "foo")]]
    

    (similar to what @biziclop said).

    If the text you’re looking might be split up across multiple elements / comments / etc. — then you can use this [edited, twice]:

    //*[contains(., "foo") and not(*[contains(., "foo")])]
    

    But that’s fairly inefficient. The following is not guaranteed to work:

    //*[contains(., "foo")][1]
    

    It will give you [edited, twice] every element that is a first child of its parent that (is an ancestor of one that) contains the text. (Or an empty nodeset, if “foo” is not found.) I’m trusting @Alejandro on this one… I still have not internalized how to tell when [position() = x] applies to the most recent location step only. Regardless, this XPath expression is not guaranteed to give you the right result.

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

Sidebar

Related Questions

Hey Forum, So i'm trying to find out how to use the source files
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey all, i am trying to create a report to show how much is
Hey, I'm trying to get the source code of an app similar to the
Hey I'm trying to creare a link that passes the link's text directly to
Hey so what I want to do is snag the content for the first
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, in the Programming Pearls book, there is a source code for setting, clearing
Hey having some trouble trying to maintain transparency on a png when i create
Hey there, I'm at the moment trying to make a product management application. The

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.