php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81468 Inconsistent default namespace inheritance
Submitted: 2021-09-23 09:03 UTC Modified: 2024-03-09 16:09 UTC
From: dennis dot verspuij+php at gmail dot com Assigned: nielsdos (profile)
Status: Closed Package: DOM XML related
PHP Version: 8.0.10 OS:
Private report: No CVE-ID: None
 [2021-09-23 09:03 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-23 11:21 UTC] 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 11:21 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2023-01-21 09:19 UTC] rajeshvarma201819 at gmail dot com
Hey my friend suggest me this spa body ma ssage website for Mangalore (https://bit.ly/3iTQl0w)github.com
 [2024-03-09 16:09 UTC] nielsdos@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nielsdos
 [2024-03-09 16:09 UTC] nielsdos@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 21:01:28 2024 UTC