php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25838 Dom_Node->child_nodes() is not live as the W3C DOM specification demands
Submitted: 2003-10-11 12:23 UTC Modified: 2003-10-15 00:07 UTC
From: Martin dot Honnen at arcor dot de Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 4.3.3 OS: Windows XP
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:
21 - 11 = ?
Subscribe to this entry?

 
 [2003-10-11 12:23 UTC] Martin dot Honnen at arcor dot de
Description:
------------
The W3C DOM Level 2 specification at
  http://www.w3.org/TR/DOM-Level-2-Core/core.html#td-live
says about NodeList collections that they should be live, meaning

if a DOM user gets a NodeList object containing the children of an Element, then subsequently adds more children to that element (or removes children, or modifies them), those changes are automatically reflected in the NodeList, without further action on the user's part.

However my test with PHP 4.3.3 and the following settings for DOMXML

DOM/XML 	enabled
DOM/XML API Version 	20020815
libxml Version 	20507
HTML Support 	enabled
XPath Support 	enabled
XPointer Support 	enabled
DOM/XSLT 	enabled
libxslt Version 	1.0.30
libxslt compiled against libxml Version 	2.5.7

shows that the result returned from Node->child_nodes() is not live but static.

Reproduce code:
---------------
<?php
function dumpDoc ($xmlDocument) {
  echo "<p><pre>\n";
  echo htmlentities($xmlDocument->dump_mem(true));
  echo "</pre></p>\n";
}
$xmlDocument = domxml_new_doc("1.0");
$rootElement = $xmlDocument->create_element("gods");
$xmlDocument->append_child($rootElement);
$childNodes = $rootElement->child_nodes();
echo 'count($childNodes): ' . count($childNodes) . "<br>\n";
$xmlDocument->append_child($rootElement);
for ($i = 0; $i < 5; $i++) {
  $god = $xmlDocument->create_element("god");
  $god->set_attribute("name", "god $i");
  $rootElement->append_child($god);
  dumpDoc($xmlDocument);
  echo 'count($childNodes): ' . count($childNodes) . "<br>\n";
}
?>

Expected result:
----------------
I would expect count($childNodes) to be incremented every time $rootElement->append_child($god) is called. That is what the W3C DOM understands to be a live collection, and that is what happens with conformant DOM implementations (as the one in Mozilla or the one in SUN's Java SDK 1.4).

Actual result:
--------------
The output with dump_mem shows that child elements are added but the output of count($childNodes) is always 0.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-12 22:15 UTC] sniper@php.net
It works fine when you actually set the $childNodes variable inside the for() loop.

 [2003-10-13 05:29 UTC] Martin dot Honnen at arcor dot de
But the DOM specification says "changes are automatically reflected in the NodeList, without further action on the user's part". If you have to call child_nodes() again and again to have it updated then changes are not automatically reflected in the NodeList. Compare a simple JavaScript program to be run in Mozilla:

var xmlDocument = document.implementation.createDocument('', '', null);
var rootElement = xmlDocument.createElement('gods');
xmlDocument.appendChild(rootElement);
var childNodes = rootElement.childNodes;
for (var i = 0; i < 5; i++) {
  var god = xmlDocument.createElement('god');
  god.setAttribute('name', 'god ' + i);
  rootElement.appendChild(god);
  alert(childNodes.length);
}

You will find that childNodes.length is incremented although childNodes is created outside of the loop.
 [2003-10-15 00:07 UTC] chregu@php.net
Just for your Information:

Once we have implemented NodeLists in the new ext/dom for PHP5, this should work as expected by the W3C.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 12:01:30 2024 UTC