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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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
 [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
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Thu Mar 30 20:03:37 2023 UTC