php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66359 Corrupt implementation of Traversable in DOMNodeList, when created by DOMXPath
Submitted: 2013-12-27 11:30 UTC Modified: 2018-11-28 17:24 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: r dot wilczek at web-appz dot de Assigned: cmb (profile)
Status: Closed Package: DOM XML related
PHP Version: 5.5.7 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
46 - 2 = ?
Subscribe to this entry?

 
 [2013-12-27 11:30 UTC] r dot wilczek at web-appz dot de
Description:
------------
A DOMNodeList created by evaluating a DOMXPath is not correctly traversable.
Equivalent instances of DOMNodeList created by getElementsByTagName() etc. do not have this problem.

The example below creates equivalent nodelists and shows the incorrect iteration of the one created by evaluating an XPath-expression.

Test script:
---------------
function printList(\DOMNodeList $list)
{
    foreach ($list as $outer) {
        /* @var $outer \DOMNode */
        echo 'outer: ' . $outer->nodeValue . PHP_EOL;
        foreach ($list as $inner) {
            /* @var $inner \DOMNode */
            echo ' inner: ' . $inner->nodeValue . PHP_EOL;
        }
    }
}

/**
 * @return DOMNodeList
 */
function getByXpath(\DOMDocument $dom)
{
    return (new \DOMXPath($dom))->evaluate('item');
}
/**
 * @return DOMNodeList
 */
function getByTagName(\DOMDocument $dom)
{
    return $dom->getElementsByTagName('item');
}

$xml = <<< XML
<root>
  <item>1</item>
  <item>2</item>
  <item>3</item>
</root>
XML;

$dom = new \DOMDocument;
$dom->loadXml($xml);

printList(getByTagName($dom));
echo '-------------' . PHP_EOL;
printList(getByXpath($dom));



Expected result:
----------------
outer: 1
 inner: 1
 inner: 2
 inner: 3
outer: 2
 inner: 1
 inner: 2
 inner: 3
outer: 3
 inner: 1
 inner: 2
 inner: 3
-------------
outer: 1
 inner: 1
 inner: 2
 inner: 3
outer: 2
 inner: 1
 inner: 2
 inner: 3
outer: 3
 inner: 1
 inner: 2
 inner: 3

Actual result:
--------------
outer: 1
 inner: 1
 inner: 2
 inner: 3
outer: 2
 inner: 1
 inner: 2
 inner: 3
outer: 3
 inner: 1
 inner: 2
 inner: 3
-------------
outer: 1
 inner: 1
 inner: 2
 inner: 3


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-30 16:35 UTC] jpauli@php.net
DomXpath and DomDocument don't set the node to the same type.
Iterator uses the type to dispatch a different iteration method (http://lxr.php.net/xref/PHP_5_5/ext/dom/dom_iterators.c#203)

DomDocument iterator creation : http://lxr.php.net/xref/PHP_5_5/ext/dom/php_dom.c#1114
DomXpath iterator creation :
http://lxr.php.net/xref/PHP_5_5/ext/dom/xpath.c#dom_xpath_iter
 [2014-01-30 20:03 UTC] r dot wilczek at web-appz dot de
And is that expected behaviour, or would you confirm this as a bug?
 [2015-03-30 10:20 UTC] r dot wilczek at web-appz dot de
Is there anybody paying attention to this? I cannot see, why the fact, that DOMXPath and DOMDocument use different iterators, justifies itself.
 [2018-11-28 17:24 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-11-28 17:24 UTC] cmb@php.net
Fixed as of PHP 7.3.0[1].  Closing as duplicate of bug #75451.

[1] <https://3v4l.org/FXAuR>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 20:01:32 2024 UTC