Bug #81468 [Ver->Csd]: Inconsistent default namespace inheritance
Edit report at https://bugs.php.net/bug.php?id=81468&edit=1
ID: 81468
Updated by: nielsdos@php.net
Reported by: dennis dot verspuij+php at gmail dot com
Summary: Inconsistent default namespace inheritance
-Status: Verified
+Status: Closed
Type: Bug
Package: DOM XML related
PHP Version: 8.0.10
-Assigned To:
+Assigned To: nielsdos
Block user comment: N
Private report: N
New Comment:
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.
This is fixed in the new opt-in spec-compliance mode, which was merged in https://github.com/php/php-src/pull/13031.
For more information about the opt-in mode, please see https://wiki.php.net/rfc/opt_in_dom_spec_compliance.
In short: the behaviour of the old DOM classes are unaffected for backwards-compatibility reasons.
There are new DOM classes where your code is correctly handled.
This feature will be available in PHP 8.4.
Previous Comments:
------------------------------------------------------------------------
[2023-01-21 09:19:02] rajeshvarma201819 at gmail dot com
Hey my friend suggest me this spa body ma ssage website for Mangalore
(https://bit.ly/3iTQl0w)github.com
------------------------------------------------------------------------
[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
Thread (5 messages)