|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52373 XPath returns inconsistent results
Submitted: 2010-07-19 01:37 UTC Modified: 2013-12-03 08:15 UTC
Avg. Score:4.0 ± 1.0
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: david at sickmiller dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.2.13 OS: Cent OS 5
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: david at sickmiller dot com
New email:
PHP Version: OS:


 [2010-07-19 01:37 UTC] david at sickmiller dot com
Sometimes the DOMXpath->evaluate() function returns unreliable results.  This seems to happen when the document has at least a depth of 3 and selectors are used at multiple levels.  There may be other conditions that trigger it; I'm not sure.  The test script below is consistently showing errors for me.

Test script:
$rawxml = '<?xml version="1.0"?><root><field attr1="val1"><field attr1="val1"/></field></root>';
$rawxpath = "/root[1]/field[@attr1='val1']";

$results = array();
for ($i = 0; $i < 10000; $i++) {
        $xmlDocument = new DOMDocument();
        $xp = new DOMXPath($xmlDocument);
        $xpr = $xp->evaluate($rawxpath, $xmlDocument->documentElement);

        if (isset($results[(string) $xpr->length]))
                $results[(string) $xpr->length]++;
                $results[(string) $xpr->length] = 1;

foreach ($results as $matches => $count)
        printf("In %5d instances, found $matches match(es) for $rawxpath\n", $count);

Expected result:
In 10000 instances, found 1 match(es) for /root[1]/field[@attr1='val1']

Actual result:
The exact numbers seem to vary, but I get roughly this response:

In  9924 instances, found 1 match(es) for /root[1]/field[@attr1='val1']
In    76 instances, found 2 match(es) for /root[1]/field[@attr1='val1']


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-19 07:59 UTC] m dot philipp at coreto dot de
Cannot repeat with php 5.3.2.
All test runs resulted in: 
"In 10000 instances, found 1 match(es) for /root[1]/field[@attr1='val1']"
 [2010-07-19 09:09 UTC] david at sickmiller dot com
I wonder if this may be related to the libxml2 library.  I am using v2.6.26- from the CentOS distribution.
 [2010-10-06 16:13 UTC] bugs dot php dot net at moesen dot nu
I cannot reproduce this either.

$ cat /etc/issue
Debian GNU/Linux squeeze/sid \n \l

$ php --version
PHP 5.3.3-0.dotdeb.0 with Suhosin-Patch (cli) (built: Jul 24 2010 01:12:50) 
$ dpkg --list | grep libxml2
ii  libxml2         2.7.7.dfsg-2         GNOME XML library
 [2013-12-03 08:15 UTC]
-Status: Open +Status: Not a bug
 [2013-12-03 08:15 UTC]
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Jul 27 10:01:23 2021 UTC