|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55218 missing a way to know if namespace was declared on current node
Submitted: 2011-07-16 18:45 UTC Modified: 2012-08-06 01:20 UTC
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: jerikojerk at gmail dot com Assigned: stas (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.3SVN-2011-07-16 (snap) OS: ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
46 - 6 = ?
Subscribe to this entry?

 [2011-07-16 18:45 UTC] jerikojerk at gmail dot com
From manual page:

There is no way to know when you're on a SimpleXMLElement, if there were a new namespace declared on current node. 

It's due to getDocNamespaces or getNamespaces returning xmlns status for all the document or document root only.

It's conform to what is described in the documentation but as user we may expect a different behavior.

Test script:

$x = new SimpleXMLElement(
'<?xml version="1.0" standalone="yes"?>
<people xmlns:p="" >
    <person id="1" xmlns:t="" >
		<name>John Doe</name>
    <person id="2">Susie Q. Public</person>
		<div>jdslkfjsldk jskdfjsmlkjfkldjkjflskj kljfslkjf sldk</div>
//echo '<pre>',$x->asXML(),'</pre>';
echo "\n<br>recursive: <br>\n";
//$tmp = $x->getNamespaces(true) ;
$tmp = $x->getDocNamespaces(true) ;
echo "\n<br>\n";
//$tmp = $x->person[0]->getNamespaces(true) ;
$tmp = $x->person[0]->getDocNamespaces(true) ;
echo "\n<br>\n";
//$tmp = $x->person[1]->getNamespaces(true) ;
$tmp = $x->person[1]->getDocNamespaces(true) ;
echo "\n<br>non recursive: <br>\n";
//$tmp = $x->getNamespaces(false) ;
$tmp = $x->getDocNamespaces(false) ;
echo "\n<br>\n";
//$tmp = $x->person[0]->getNamespaces(false) ;
$tmp = $x->person[0]->getDocNamespaces(false) ;
echo "\n<br>\n";
//$tmp = $x->person[1]->getNamespaces(false) ;
$tmp = $x->person[1]->getDocNamespaces(false) ;

Expected result:
if we were able to provide namespace declare only on current node instead of root node, the function will provide the following result

array(2) { ["p"]=> string(20) "" ["t"]=> string(20) "" }
array(1) { ["t"]=> string(20) "" }
array(0) {  }
non recursive:
array(1) { ["p"]=> string(20) "" }
array(1) { ["t"]=> string(20) "" }
array(0) {  } 

Actual result:
array(2) { ["p"]=> string(20) "" ["t"]=> string(20) "" }
array(2) { ["p"]=> string(20) "" ["t"]=> string(20) "" }
array(2) { ["p"]=> string(20) "" ["t"]=> string(20) "" }
non recursive:
array(1) { ["p"]=> string(20) "" }
array(1) { ["p"]=> string(20) "" }
array(1) { ["p"]=> string(20) "" } 


add_option_param_to_getDocNamespaces_phpt (last revision 2011-07-23 03:16 UTC by lonnyk at gmail dot com)
add_optional_param_to_getDocNamespaces (last revision 2011-07-23 03:14 UTC by lonnyk at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-22 23:20 UTC] lonnyk at gmail dot com
I modified the definition of getDocNamespaces to be:

public array SimpleXMLElement::getDocNamespaces ([ bool $recursive = false [, 
bool $from_root = true ] ] )

If the second param is set to true then the method performs exactly as it does 
now. If it is set to false it performs as requested.

I choose to modify the method instead of creating a new one because I feel this 
is more of a configuration option for the same functionality. If anyone has a 
different opinion please let me know.
 [2012-06-19 22:18 UTC] renaud dot savalle at gmail dot com
I am working with complex XML files with namespaces declarations spread across different elements. In particular I need to be able to test if a particular namespace is declared in a certain node, and as pointed out by the requester, there is currently no way to do that with SimpleXMLElement. Therefore I would find Lonny's patch useful for my work and wish it could be included into PHP.
 [2012-06-28 15:37 UTC] lonnyk at gmail dot com
Updated the patch on GitHub
 [2012-08-06 01:20 UTC]
Merged onto 5.4 and master.

Assigning to Stas because he merged the pull request, but the patch itself came from Lonny Kapelushnik.
 [2012-08-06 01:20 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: stas
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Aug 03 11:01:28 2020 UTC