Skip to content

getting SimpleXML doc namespaces #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ PHP NEWS
. Fixed bug #62202 (ReflectionParameter::getDefaultValue() memory leaks
with constant). (Laruence)

- SimpleXMLElement:
. Implemented FR #55218 Get namespaces from current node. (Lonny)

- Sockets:
. Fixed bug #62025 (__ss_family was changed on AIX 5.3). (Felipe)

Expand Down
4 changes: 4 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ PHP 5.4 UPGRADE NOTES

- Since 5.4.5, resourcebundle_create() accepts null for the first two arguments.

- Since 5.4.5, SimpleXMLElement::getDocNamespaces() has and extra parameter which
allows for toggling if the list of namespaces starts from the document root
or from the node you call the method on

==============================
5. Changes to existing classes
==============================
Expand Down
21 changes: 16 additions & 5 deletions ext/simplexml/simplexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1513,22 +1513,28 @@ static void sxe_add_registered_namespaces(php_sxe_object *sxe, xmlNodePtr node,
}
/* }}} */

/* {{{ proto string SimpleXMLElement::getDocNamespaces([bool recursive])
/* {{{ proto string SimpleXMLElement::getDocNamespaces([bool recursive [, bool from_root])
Return all namespaces registered with document */
SXE_METHOD(getDocNamespaces)
{
zend_bool recursive = 0;
zend_bool recursive = 0, from_root = 1;
php_sxe_object *sxe;
xmlNodePtr node;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &recursive) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &recursive, &from_root) == FAILURE) {
return;
}

array_init(return_value);

sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
if(from_root){
node = xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr);
}else{
GET_NODE(sxe, node);
}

sxe_add_registered_namespaces(sxe, xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr), recursive, return_value TSRMLS_CC);
sxe_add_registered_namespaces(sxe, node, recursive, return_value TSRMLS_CC);
}
/* }}} */

Expand Down Expand Up @@ -2518,6 +2524,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_getnamespaces, 0, 0, 0)
ZEND_ARG_INFO(0, recursve)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_getdocnamespaces, 0, 0, 0)
ZEND_ARG_INFO(0, recursve)
ZEND_ARG_INFO(0, from_root)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_children, 0, 0, 0)
ZEND_ARG_INFO(0, ns)
ZEND_ARG_INFO(0, is_prefix)
Expand Down Expand Up @@ -2586,7 +2597,7 @@ static const zend_function_entry sxe_functions[] = { /* {{{ */
SXE_ME(attributes, arginfo_simplexmlelement_children, ZEND_ACC_PUBLIC)
SXE_ME(children, arginfo_simplexmlelement_children, ZEND_ACC_PUBLIC)
SXE_ME(getNamespaces, arginfo_simplexmlelement_getnamespaces, ZEND_ACC_PUBLIC)
SXE_ME(getDocNamespaces, arginfo_simplexmlelement_getnamespaces, ZEND_ACC_PUBLIC)
SXE_ME(getDocNamespaces, arginfo_simplexmlelement_getdocnamespaces, ZEND_ACC_PUBLIC)
SXE_ME(getName, arginfo_simplexmlelement__void, ZEND_ACC_PUBLIC)
SXE_ME(addChild, arginfo_simplexmlelement_addchild, ZEND_ACC_PUBLIC)
SXE_ME(addAttribute, arginfo_simplexmlelement_addchild, ZEND_ACC_PUBLIC)
Expand Down
117 changes: 117 additions & 0 deletions ext/simplexml/tests/feature55218.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
--TEST--
Bug #55218 getDocNamespaces from current element and not root
--SKIPIF--
<?php
if (!extension_loaded("simplexml")) print "skip SimpleXML not present";
if (!extension_loaded("libxml")) print "skip LibXML not present";
?>
--FILE--
<?php

$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" >
<t:name>John Doe</t:name>
</person>
<person id="2">Susie Q. Public</person>
<o>
<p:div>jdslkfjsldk jskdfjsmlkjfkldjkjflskj kljfslkjf sldk</p:div>
</o>
</people>');

echo "getDocNamespaces\n";
echo "\nBackwards Compatibility:\n";
echo "recursion:\n";

var_dump ( $x->getDocNamespaces(true) ) ;
var_dump( $x->person[0]->getDocNamespaces(true) );
var_dump( $x->person[1]->getDocNamespaces(true) );

echo "\nnon recursive:\n";

var_dump( $x->getDocNamespaces(false) );
var_dump( $x->person[0]->getDocNamespaces(false) );
var_dump( $x->person[1]->getDocNamespaces(false) );

echo "\n\nUsing new 'from_root' bool set to false:\n";
echo "recursion:\n";

var_dump ( $x->getDocNamespaces(true, false) ) ;
var_dump( $x->person[0]->getDocNamespaces(true, false) );
var_dump( $x->person[1]->getDocNamespaces(true, false) );

echo "\nnon recursive:\n";

var_dump( $x->getDocNamespaces(false, false) );
var_dump( $x->person[0]->getDocNamespaces(false, false) );
var_dump( $x->person[1]->getDocNamespaces(false, false) );

?>
===DONE===
--EXPECTF--
getDocNamespaces

Backwards Compatibility:
recursion:
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"
}


Using new 'from_root' bool set to false:
recursion:
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) {
}
===DONE===