Bug #81468 [Opn->Ver]: Inconsistent default namespace inheritance
| From: | cmb@php.net | Date: | Thu, 23 Sep 2021 11:21:48 +0000 |
| Subject: | Bug #81468 [Opn->Ver]: Inconsistent default namespace inheritance | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-236771@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81468&edit=1
ID: 81468
Updated by: cmb@php.net
Reported by: dennis dot verspuij+php at gmail dot com
Summary: Inconsistent default namespace inheritance
-Status: Open
+Status: Verified
Type: Bug
Package: DOM XML related
PHP Version: 8.0.10
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2021-09-23 11:21:41] cmb@php.net
This inconsistency[1] is a bug, and I think it's an issue with our
use of libxml2, although I'm not absolutely sure. We may miss to
call some libxml2 API here, or maybe we need to "fix" the missing
namespace in dom_reconcile_ns()[2] manually.
Also note, that I think the different behavior is not related to
saving the document, but rather that reloading the document fixes
the DOM.
[1] <https://3v4l.org/iLfmV>
[2] <https://github.com/php/php-src/blob/php-7.4.24/ext/dom/node.c#L160>
------------------------------------------------------------------------
[2021-09-23 09:03:56] dennis dot verspuij+php at gmail dot com
Description:
------------
A child node created with DOMDocument::createElement() added to an element with a non-empty default
namespace internally does not seem to inherit the default namespace of the parent, while when saving
the XML it looks like it does.
And imho it should inherit the namespace, because a node created with createElement() does not yet
have a namespace until it is added somewhere in the document (hence parameter name localName). In
contrary a node created with createElementNS() keeps its explicit namespace once added anywhere.
Not sure if this a libxml bug, or a bug in PHP xml extension.
Test script:
---------------
<?php
$dom = new \DOMDocument();
$dom
->appendChild($dom->createElementNS('some:namespace', 'foo'))
->appendChild($dom->createElement('bar'));
echo ($xml = $dom->saveXML());
$xpath = new \DOMXPath($dom);
$xpath->registerNamespace('n', 'some:namespace');
echo count($xpath->query('/n:foo/bar')) . " should be 0\n";
echo count($xpath->query('/n:foo/n:bar')) . " should be 1\n\n";
//
$dom = new \DOMDocument();
$dom->loadXml($xml);
echo ($xml = $dom->saveXML());
$xpath = new \DOMXPath($dom);
$xpath->registerNamespace('n', 'some:namespace');
echo count($xpath->query('/n:foo/bar')) . " should be 0\n";
echo count($xpath->query('/n:foo/n:bar')) . " should be 1\n\n";
Expected result:
----------------
See test script and output.
Actual result:
--------------
See test script and output.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81468&edit=1