php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39365 getElementsByTagNameNS() does not return elements in a default namespace
Submitted: 2006-11-03 21:12 UTC Modified: 2006-11-04 21:42 UTC
From: z_rules55 at hotmail dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.2.0 OS: WinXP Professional
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: z_rules55 at hotmail dot com
New email:
PHP Version: OS:

 

 [2006-11-03 21:12 UTC] z_rules55 at hotmail dot com
Description:
------------
Calling getElementsByTagNameNS() on a DOMDocument or a DOMElement does not return elements that are under a default namespace. The example below finds $explicit_ns_element, but not $default_ns_element.

Reproduce code:
---------------
<?php
$xml = new DOMDocument();
$namespace = 'my_namespace';
$root = $xml->appendChild($xml->createElementNS($namespace, 'root'));
$default_ns_element = $root->appendChild($xml->createElement('element', 'default_ns_element'));
$explicit_ns_element = $root->appendChild($xml->createElementNS($namespace, 'element', 'explicit_ns_element'));
foreach($xml->getElementsByTagNameNS($namespace, 'element') as $el) {
	echo $el->nodeValue."\n";
}
echo "\n";
foreach($root->getElementsByTagNameNS($namespace, 'element') as $el) {
	echo $el->nodeValue."\n";
}
?>

Expected result:
----------------
default_ns_element
explicit_ns_element

default_ns_element
explicit_ns_element

Actual result:
--------------
explicit_ns_element

explicit_ns_element

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-03 21:28 UTC] z_rules55 at hotmail dot com
Additional note: getElementsByTagName('element') does, in fact, find both nodes.
 [2006-11-04 08:03 UTC] chregu@php.net
$xml->createElement('element', 'default_ns_element')

That's not in the default namespace, that's in no namespace at 
all this way.

Can't work this way
 [2006-11-04 14:26 UTC] z_rules55 at hotmail dot com
Per the XML spec, setting the xmlns attribute on an element but with no prefix (like I did with $root) sets a default namespace for that element and its descendants. $default_ns_element and $explicit_ns_element, therefore, do not need the xmlns attribute to be in 'my_namespace' because they inherit the namespace from $root by default.
 [2006-11-04 21:42 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You are mixing level 1 and level 2 functionality. Specs also indicate that doing this will lead to unexpected behavior. Must use createElementNS().
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 13:01:33 2025 UTC