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: 2021-09-23 11:21 UTC
From: dennis dot verspuij+php at gmail dot com Assigned:
Status: Verified Package: DOM XML related
PHP Version: 8.0.10 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dennis dot verspuij+php at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 27 09:03:33 2021 UTC