Edit report at https://bugs.php.net/bug.php?id=55218&edit=1
ID: 55218
Comment by: lonnyk at gmail dot com
Reported by: jerikojerk at gmail dot com
Summary: missing a way to know if namespace was declared on
current node
Status: Open
Type: Feature/Change Request
Package: SimpleXML related
Operating System: ubuntu
PHP Version: 5.3SVN-2011-07-16 (snap)
Block user comment: N
Private report: N
New Comment:
Updated the patch on GitHub https://github.com/php/php-src/pull/112
Previous Comments:
------------------------------------------------------------------------
[2012-06-19 22:18:55] 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.
------------------------------------------------------------------------
[2011-07-22 23:20:24] 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.
------------------------------------------------------------------------
[2011-07-16 18:45:43] 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" }
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55218&edit=1