php.net |  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
Votes:2
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
 [2011-07-16 18:45 UTC] jerikojerk at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/simplexmlelement.getdocnamespaces%23Voir%20aussi
---


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:
---------------
<?php
//phpinfo();

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



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

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



Actual result:
--------------
recursive:
array(2) { ["p"]=> string(20) "http://example.org/p" ["t"]=> string(20) "http://example.org/t" }
array(2) { ["p"]=> string(20) "http://example.org/p" ["t"]=> string(20) "http://example.org/t" }
array(2) { ["p"]=> string(20) "http://example.org/p" ["t"]=> string(20) "http://example.org/t" }
non recursive:
array(1) { ["p"]=> string(20) "http://example.org/p" }
array(1) { ["p"]=> string(20) "http://example.org/p" }
array(1) { ["p"]=> string(20) "http://example.org/p" } 

Patches

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)

Pull Requests

History

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 https://github.com/php/php-src/pull/112
 [2012-08-06 01:20 UTC] aharvey@php.net
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] aharvey@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: stas
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 01:01:30 2024 UTC