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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:58:31+00:00 2026-05-23T11:58:31+00:00

DOMXPath $html->query(‘//p[@class=myclass]/a’)->item(0); is not working. Here is the HTML: <p class=username> <img src=http://static1.tryal.com/img/b.gif onclick=lalalaa()

  • 0

DOMXPath $html->query('//p[@class="myclass"]/a')->item(0); is not working.

Here is the HTML:

<p class="username">
<img src="http://static1.tryal.com/img/b.gif" onclick="lalalaa()" class="a12s_iYblt4hN_DkIt_GoiyOtu8 opsi" />
<b>
<a href="/about"><b>Lalala.</b></a>
</b>
</p>


$name = $html->query('//p[@class="username"]/a')->item(0)->nodeValue; 
//This doesn't return the name "Lalala.";

$name = $html->query('//p[@class="username"]')->item(0)->nodeValue; 
//This works just fine.

Why isn’t this tree working? Am I typing it wrong?

Thank you very much in advance.

  • 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-23T11:58:32+00:00Added an answer on May 23, 2026 at 11:58 am

    The xpath you have specified didn’t provide any result (or an result with no elements to be precise). You can fix that by not letting the b element slip through:

    $name = $html->query('//p[@class="username"]/b/a')->item(0)->nodeValue; 
                                                ^^^
    

    or by not making a a direct/immediate child from p but somewhere deeper as well (descendant of, double slash // in xpath):

    $name = $html->query('//p[@class="username"]//a')->item(0)->nodeValue; 
                                                ^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML block here: <div class=title> <a href=http://test.com/asus_rt-n53/p195257/> Asus RT-N53 </a> </div>
$dom= new DOMDocument('1.0', 'iso-8859-1'); $dom->loadHTML(' <html><body> <strong>SECOND</strong> </body></html>'); $path = new DOMXPath($dom); foreach($path->query('body') as
I have this code: $reader = new DOMDocument(); $reader->loadHTML($shell); $xpath = new DomXPath($reader); $xpath->registerNamespace('html','http://www.w3.org/1999/xhtml');
$xpath = new DOMXpath($doc); $res = $xpath->query(.//*[@id='post2679883']/tr[2]/td[2]/div[2]); foreach( $res as $obj ) { var_dump($obj->nodeValue);
I have a HTML document (string) which contains a div with the class foo:
Here is my code below. $dom = new DOMDocument(); $dom->loadHTML($source); // Html String $xpath
This is my example script: $html = <<<HTML <div class=main> <div class=text> Capture this
So far I'm working on a HTML file like this <div name=node></div> <div></div> <div
I have a problem with a php DOM Object http://php.net/manual/en/class.domdocument.php Is it possible only
Say I have an html file that I have loaded, I run this query:

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.