Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

getting SimpleXML doc namespaces #112

Merged
merged 3 commits into from

2 participants

@lonnylot

I was recently emailed asking to have the patch for bug 55218 added in. I updated it and fixed a minor bug.

Currently if you recursively get an XML docs namespaces via SimpleXML::getDocNamespaces it will always start from the root regardless of what node you call the method from.

The patch allows you to recursively get the XML doc namespaces starting from the node you pass in.

We add in a second parameter to the SimpleXML::getDocNamespaces method called 'from_root'. This param defaults to 'true' to keep backwards compatibility. If set to 'false' then we only retrieve the namespace(s) for the node the method is called from.

Lonny Kapelu... added some commits
Lonny Kapelushnik Implements feature 55218
Allows you to recursively check namespaces under a child node
instead of from the root of the XML doc
f1ef8b3
Lonny Kapelushnik Adding in test for feature 55218
2178b1d
Lonny Kapelushnik Added in NEWS and UPGRADING for feature 55218
f0df7db
@php-pulls php-pulls merged commit f0df7db into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 22, 2012
  1. Implements feature 55218

    Lonny Kapelushnik authored
    Allows you to recursively check namespaces under a child node
    instead of from the root of the XML doc
  2. Adding in test for feature 55218

    Lonny Kapelushnik authored
Commits on Jul 7, 2012
  1. Added in NEWS and UPGRADING for feature 55218

    Lonny Kapelushnik authored
This page is out of date. Refresh to see the latest.
View
3  NEWS
@@ -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)
View
4 UPGRADING
@@ -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
==============================
View
21 ext/simplexml/simplexml.c
@@ -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);
}
/* }}} */
@@ -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)
@@ -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)
View
117 ext/simplexml/tests/feature55218.phpt
@@ -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===
Something went wrong with that request. Please try again.