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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:35:36+00:00 2026-06-07T06:35:36+00:00

I am trying to get the text from a specific node’s parent. For example:

  • 0

I am trying to get the text from a specific node’s parent. For example:

<td colspan="1" rowspan="1">
  <span>
    <a class="info" shape="rect" 
             rel="empLinkData" href="/employee.htm?id=8468524">
        Jack Johnson
    </a>
  </span>
   (*)&nbsp;
</td>

I am able to successfully process the anchor tag by using:

$xNodes = $xpath->query('//a[@class="info"][@rel="empLinkData"]');

// $xNodes contains employee ids and names
foreach ($xNodes as $xNode)
{
    $sLinktext = @$xNode->firstChild->data;
    $sLinkurl = 'http://www.company.com' . $xNode->getAttribute('href');

    if ($sLinktext != '' && $sLinkurl != '')
    {
        echo '<li><a href="' . $sLinkurl . '">' .
                $sLinktext . '</a></li>';
    }
}

Now, I need to retrieve the text from the <td> tag (in this case, the (*)&nbsp; appearing right after the span tag closes), but I can’t seem to refer to it properly.

The xpath for this that seems to make the most sense to me is:

$xNodes = $xpath->query('//a[@class="info"]
          [@rel="empLinkData"]/ancestor::*');

but it is retrieving the wrong data from elsewhere nested above this code.

  • 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-07T06:35:38+00:00Added an answer on June 7, 2026 at 6:35 am

    It’s not necessary to retreat back up the tree. Instead, directly select the td that contains the relevant element:

    //td[descendant::a[@class="info"][@rel="empLinkData"]]/text()
    

    Edit: As @Dimitre rightly pointed out, this selects all text children. Your td has two such nodes: the whitespace-only text node that precedes the span and the text node that follows it. If you only want the second text node, then use:

    //td[descendant::a[@class="info"][@rel="empLinkData"]]/text()[2]
    

    Or:

    //td[descendant::a[@class="info"][@rel="empLinkData"]]/text()[last()]
    

    As you can see, the resulting expressions are essentially the same, but you do need to target the correct text node (if you want only one). Note also that if the target text is truly in a td then it’s safer to target that element type directly (without wildcards). As this is HTML, your actual document almost certainly contains several other elements, including multiple other anchors that you may not want to target.

    Sample PHP:

    $nodes = $xpath->query(
        '//td[descendant::a[@class="info"][@rel="empLinkData"]]/text()[last()]');
    echo "[". $nodes->item(0)->nodeValue . "]";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to get specific text from a plain-text organized by tags or whatever;
I am trying to get text from a textbox in a specific window. For
I'm trying to get the text from a textbox as a user types, so
I am trying to get text input from the keyboard in Java 6. I
I'm trying to get the plain text from a word document. Specifically, the xpath
I'm trying to get all the text from a txt file into a string,
I am trying to get the text inside a specific div in a server
From a text file I am trying to get the the 3rd set of
I'm trying to get a specific cell from a table view so I can
I am trying to replace specific highlighted(marked) text from element. This is how I

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.