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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:17:20+00:00 2026-05-17T23:17:20+00:00

I am using PHP to retrieve content for a given URL and XPATH. I

  • 0

I am using PHP to retrieve content for a given URL and XPATH.
I use DOMDocument / DOMXPath (with query or evaluate).

For small xpath, I obtain correct result, but for longer xpath, it does not work. (And this xpath seems to be good (I obtained them with Xpather (firefox plugin) and re-test them with YQL).

Do you have any advice on this curious trouble ?

Example of code:

$doc = new DOMDocument();
$myXMLString = file_get_contents('http://stackoverflow.com/questions/4097230/too-long-xpath-with-domxpath-query-evaluate-return-nothing');
@$doc->loadHTML($myXMLString); //@ to suppress warnings 
                               //(good for not ending markup)
$xpath = new DOMXPath($doc);

$fullPath ="/html/body/small/path"; //it works
//$fullPath = "/html/body/full/path/with/lot/of/markup";//does not works
$entries = $xpath->query($fullPath);
//or ->evalutate($fullPath) (same behaviour)
//$entries return DOMNodeList (empty for a long path query, 
//                             correct for a small path query)

I test with attribute restriction, but is seems to not change (with small xpath it works, with longer it do not works more)

Example :
for this current page:

$fullPath = "/html
              /body
               /div[4]
                /div[@id='content']
                 /div[@id='question-header']
                  /h1
                   /a";//works (retrieve the question title)
$fullPath = "/html
              /body
               /div[4]
                /div[@id='content']
                 /div[@id='mainbar']
                  /div[@id='question']
                   /table
                    /tbody
                     /tr[2]
                      /td[2]
                       /div[@id='comments-4097230']
                        /table
                         /tbody
                          /tr[@id='comment-4408626']
                           /td[2]
                            /div
                             /a"; //does'nt work 
                                  //(should retrieve 'gaby' from comment)

Edit:

I test with SimpleXML lib, and I have exactly the same behavior (good result for small query, nothing for long query).


Edit 2:

I also cut the longest xpath by deleting some first element and it works.
BTW I really do not understand why a full correct xpath does not work.

  • 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-17T23:17:20+00:00Added an answer on May 17, 2026 at 11:17 pm

    Let’s go through this step by step:

    Step 1: replicating the error.

    After verifying that the XPath will indeed not return a result, I wrote a little script to see how deep the XPath will go before it breaks

    foreach (explode('/', $fullPath) as $segment) {
        $xpath .= trim($segment);
        echo '-------------------------------------------', PHP_EOL,
             'Trying: ', $xpath, PHP_EOL,
             '-------------------------------------------', PHP_EOL;
        echo $xp->evaluate("string($xpath)"), PHP_EOL;
        $xpath .= '/';
    }
    

    The last thing it will return a result for is

    /html/body/div[4]/div[@id='content']/div[@id='mainbar']/div[@id='question']/table
    

    Step 2: checking the markup

    So I checked the markup returned by DOMDocument::saveHTML() to see what it looks like and there was no <tbody> (reformatted for readability):

    <div id="question">
        <div class="everyonelovesstackoverflow" id="adzerk1"></div>
            <table>
                <tr><td class="votecell">
    

    I then checked this very page to see if it was DOM throwing it away or if it really does not exist. It wasn’t there. Apparently, Firebug inserts it, which would explain why you got the result with XPather (but not why you got it with YQL):

    Screenshot showing page source and apparently bugged Firebug view

    Step 3: proofchecking and conclusion

    I removed the <tbody> from the XPath and reran the script. No problems. Returns "Gaby".

    While I suspected a bug in Firebug first, Alejandro commented this would happen in IE’s DeveloperTools, too. I then suspected this to be added by JavaScript but could not verify that. After some more research Alejandro pointed me to Why does firebug add <tbody> to <table>? – it’s actually neither Firebug nor JavaScript though, but the browser’s themselves.

    So to modify my conclusion:

    Dont trust markup you see rendered in the browser, because it may be modified by the browser or other technologies. DOM will only download what is is served directly. If you run into similar issues again, you now know how to approach it though.


    Some additional sidenotes

    Unless you need to modify the markup before feeding it to DOM, you do not have to use file_get_contents() to load the content. You can use DOM’s loadHTMLFile():

    $dom->loadHTMLFile('http://www.example.com/foo.htm');
    

    Also, the proper way to suppress errors is to tell libxml to use it’s internal error handler. But instead of handling the errors, you simply clear them. This will only affect errors relating to libxml, e.g. parsing errors (as opposed to all PHP errors):

    libxml_use_internal_errors(TRUE);
    libxml_clear_errors();
    

    Finally, xPath queries can be done in relation to a context node. So while the long XPath is efficient in terms of lookup time, you could simply use getElementById() to get the deepest known node and then use an XPath against it.

    In other words:

    libxml_use_internal_errors(TRUE);
    $dom = new DOMDocument;
    $dom->loadHTMLFile('http://www.example.com/foo.htm');
    libxml_clear_errors();
    echo $xp->evaluate(
        'string(td[2]/div/a)', 
        $dom->getElementById('comment-4408626'));
    

    will return "Gaby" as well.

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

Sidebar

Related Questions

How do I retrieve the content-type of a file using PHP? I don't want
I am using PHP with Apache on Linux, with Sendmail. I use the PHP
I want to retrieve the first 10k bytes from a URL with curl (using
I'm having some issues with using PHP to convert ISO-8859-1 database content to UTF-8.
i ave been having trouble with a simple select sql query. using php PDO.
Using PHP, what's the fastest way to convert a string like this: 123 to
Using PHP and MySQL, I have a forum system I'm trying to build. What
Been using PHP/MySQL for a little while now, and I'm wondering if there are
In using PHP's DOM classes (DOMNode, DOMEElement, etc) I have noticed that they possess
I've been using PHP & MySQL for ages and am about to start using

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.