Bug #39593 [Com]: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists"
| From: | monier at reinom dot com | Date: | Wed, 05 Apr 2017 08:57:06 +0000 |
| Subject: | Bug #39593 [Com]: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists" | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-208330@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=39593&edit=1
ID: 39593
Comment by: monier at reinom dot com
Reported by: dave dot lane at gmx dot net
Summary: XPath NodeList: "Couldn't fetch DOMElement: Node no
longer exists"
Status: Wont fix
Type: Bug
Package: DOM XML related
Operating System: Linux
PHP Version: 5.2.0
Block user comment: N
Private report: N
New Comment:
Can this be turned into an exception instead of a Warning (still a warning in PHP 7.1.0-x64)?
So far, I rely on a patch that boils up the parentNode of the element, untill either reaching a
DOMDocument (meaning node is still there) or a null (meaning node has already been removed). Could
this be part of DOMElement's static methods, like a DOMElement::nodeExists(DOMElement $element)
?
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?><block>I have
<list><num>0A</num> orange and <list><num>0B</num></list>
apple</list>.</block>';
$doc = new DOMDocument();
$doc->loadXml($xml);
$xpath = new DOMXpath($doc);
$nodes = $xpath->query('//num', $doc->documentElement);
foreach ($nodes as $num) {
if ($num->textContent{0} == '0') {
// Node might be part of a removed DocumentFragment
// If so, ignore it: node has already been removed by removing one of its ancestor
for ($n = $num; !($n instanceof DOMDocument); $n = $n->parentNode) {
if ($n === null) {
// Node already removed (see PHP bug #39593) so skip it
continue 2;
}
}
// Will remove the first "list" ancestor of the "num" node
$a = $xpath->query('ancestor::list[1]', $num);
if ($a->length === 1) {
$p = $a->item(0);
// removed the "list" element
$p->parentNode->removeChild($p);
}
}
}
var_dump($doc->C14N());
?>
Sample: I remove the 1st "list" ancestor element of every "num" element that
starts with a "0". Since "list" are nested, the outer one is removed first when
"foreaching" the 0A node. Then the OB node is foreached, and its list ancestor is removed.
But that ancestor was already removed from document (because it's a descendant of a removed
node).
In real production system, I don't know what the XML is, and these nested case may occur
(actually, it's not "list" be nested "table" element, and "table"
ancestor is removed if a "td" contains a "0"; "table"s can be nested,
so removals can be nested at Runtime)
Previous Comments:
------------------------------------------------------------------------
[2017-04-05 08:56:53] monier at reinom dot com
Can this be turned into an exception instead of a Warning (still a warning in PHP 7.1.0-x64)?
So far, I rely on a patch that boils up the parentNode of the element, untill either reaching a
DOMDocument (meaning node is still there) or a null (meaning node has already been removed). Could
this be part of DOMElement's static methods, like a DOMElement::nodeExists(DOMElement $element)
?
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?><block>I have
<list><num>0A</num> orange and <list><num>0B</num></list>
apple</list>.</block>';
$doc = new DOMDocument();
$doc->loadXml($xml);
$xpath = new DOMXpath($doc);
$nodes = $xpath->query('//num', $doc->documentElement);
foreach ($nodes as $num) {
if ($num->textContent{0} == '0') {
// Node might be part of a removed DocumentFragment
// If so, ignore it: node has already been removed by removing one of its ancestor
for ($n = $num; !($n instanceof DOMDocument); $n = $n->parentNode) {
if ($n === null) {
// Node already removed (see PHP bug #39593) so skip it
continue 2;
}
}
// Will remove the first "list" ancestor of the "num" node
$a = $xpath->query('ancestor::list[1]', $num);
if ($a->length === 1) {
$p = $a->item(0);
// removed the "list" element
$p->parentNode->removeChild($p);
}
}
}
var_dump($doc->C14N());
?>
Sample: I remove the 1st "list" ancestor element of every "num" element that
starts with a "0". Since "list" are nested, the outer one is removed first when
"foreaching" the 0A node. Then the OB node is foreached, and its list ancestor is removed.
But that ancestor was already removed from document (because it's a descendant of a removed
node).
In real production system, I don't know what the XML is, and these nested case may occur
(actually, it's not "list" be nested "table" element, and "table"
ancestor is removed if a "td" contains a "0"; "table"s can be nested,
so removals can be nested at Runtime)
------------------------------------------------------------------------
[2010-08-18 11:06:20] rodolphe at metaphores dot ch
Hello,
Is there a way to determine if the node is no longer linked to his xml structure without having a
warning ?
Thx
------------------------------------------------------------------------
[2006-11-22 20:53:42] dave dot lane at gmx dot net
OK excellent, thanks for the tip. It makes sense and I did suspect it had something to do with
scope, I thought it was the NodeList that was causing the problem though.
I think this should be documented though.
------------------------------------------------------------------------
[2006-11-22 19:06:06] rrichards@php.net
The problem is that you are working with a fragment that's not attached to the document and
only have a reference to a node deep within the fragment rather than to the top element of the
fragment. When the object holding the fragment ($NodeImported) goes out of scope, the whole fragment
is destroyed - as there isn't a way to track fragments without introducing way too much
overhead. You still have the DOMElement object, just not the underlying xml data structure.
you either need to return the entire fragment from the function call or remove the nodes you want to
return from the fragment within the array.
i.e. within the foreach() loop add:
$Node->parentNode->removeChild($Node);
This behavior while not optimal is a necessary evil to prevent leaking memory while also keeping
system memory usage and performance at an acceptable level when using the DOM extension.
------------------------------------------------------------------------
[2006-11-22 17:58:31] dave dot lane at gmx dot net
Sorry description should read:
Description:
------------
When the nodes in a NodeList that is the result of an XPath query are copied into an array and
returned from a function the Nodes in the array although they exist are not valid. When the nodes in
the array are used a warning results: "Couldn't fetch DOMElement. Node no longer
exists..."
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=39593
--
Edit this bug report at https://bugs.php.net/bug.php?id=39593&edit=1