Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

getting SimpleXML doc namespaces #112

Merged
merged 3 commits into from almost 2 years ago

2 participants

Lonny Kapelushnik Account for PHP Pull Requests
Lonny Kapelushnik

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
Implements feature 55218
Allows you to recursively check namespaces under a child node
instead of from the root of the XML doc
f1ef8b3
Adding in test for feature 55218 2178b1d
Added in NEWS and UPGRADING for feature 55218 f0df7db
Account for PHP Pull Requests php-pulls merged commit f0df7db into from
Account for PHP Pull Requests php-pulls closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Jun 21, 2012
Implements feature 55218
Allows you to recursively check namespaces under a child node
instead of from the root of the XML doc
f1ef8b3
Adding in test for feature 55218 2178b1d
Jul 07, 2012
Added in NEWS and UPGRADING for feature 55218 f0df7db
This page is out of date. Refresh to see the latest.
3  NEWS
@@ -55,6 +55,9 @@ PHP                                                                        NEWS
55 55
    . Fixed bug #62202 (ReflectionParameter::getDefaultValue() memory leaks 
56 56
      with constant). (Laruence)
57 57
 
  58
+- SimpleXMLElement:
  59
+  . Implemented FR #55218 Get namespaces from current node. (Lonny)
  60
+
58 61
 - Sockets:
59 62
   . Fixed bug #62025 (__ss_family was changed on AIX 5.3). (Felipe)
60 63
     
4  UPGRADING
@@ -343,6 +343,10 @@ PHP 5.4 UPGRADE NOTES
343 343
 
344 344
 - Since 5.4.5, resourcebundle_create() accepts null for the first two arguments.
345 345
 
  346
+- Since 5.4.5, SimpleXMLElement::getDocNamespaces() has and extra parameter which
  347
+  allows for toggling if the list of namespaces starts from the document root
  348
+  or from the node you call the method on
  349
+
346 350
 ==============================
347 351
 5. Changes to existing classes
348 352
 ==============================
21  ext/simplexml/simplexml.c
@@ -1513,22 +1513,28 @@ static void sxe_add_registered_namespaces(php_sxe_object *sxe, xmlNodePtr node,
1513 1513
 }
1514 1514
 /* }}} */
1515 1515
 
1516  
-/* {{{ proto string SimpleXMLElement::getDocNamespaces([bool recursive])
  1516
+/* {{{ proto string SimpleXMLElement::getDocNamespaces([bool recursive [, bool from_root])
1517 1517
    Return all namespaces registered with document */
1518 1518
 SXE_METHOD(getDocNamespaces)
1519 1519
 {
1520  
-	zend_bool           recursive = 0;
  1520
+	zend_bool           recursive = 0, from_root = 1;
1521 1521
 	php_sxe_object     *sxe;
  1522
+	xmlNodePtr          node;
1522 1523
 
1523  
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &recursive) == FAILURE) {
  1524
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &recursive, &from_root) == FAILURE) {
1524 1525
 		return;
1525 1526
 	}
1526 1527
 
1527 1528
 	array_init(return_value);
1528 1529
 
1529 1530
 	sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
  1531
+	if(from_root){
  1532
+		node = xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr);
  1533
+	}else{
  1534
+		GET_NODE(sxe, node);
  1535
+	}
1530 1536
 
1531  
-	sxe_add_registered_namespaces(sxe, xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr), recursive, return_value TSRMLS_CC);
  1537
+	sxe_add_registered_namespaces(sxe, node, recursive, return_value TSRMLS_CC);
1532 1538
 }
1533 1539
 /* }}} */
1534 1540
 
@@ -2518,6 +2524,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_getnamespaces, 0, 0, 0)
2518 2524
 	ZEND_ARG_INFO(0, recursve)
2519 2525
 ZEND_END_ARG_INFO()
2520 2526
 
  2527
+ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_getdocnamespaces, 0, 0, 0)
  2528
+	ZEND_ARG_INFO(0, recursve)
  2529
+	ZEND_ARG_INFO(0, from_root)
  2530
+ZEND_END_ARG_INFO()
  2531
+
2521 2532
 ZEND_BEGIN_ARG_INFO_EX(arginfo_simplexmlelement_children, 0, 0, 0)
2522 2533
 	ZEND_ARG_INFO(0, ns)
2523 2534
 	ZEND_ARG_INFO(0, is_prefix)
@@ -2586,7 +2597,7 @@ static const zend_function_entry sxe_functions[] = { /* {{{ */
2586 2597
 	SXE_ME(attributes,             arginfo_simplexmlelement_children, ZEND_ACC_PUBLIC)
2587 2598
 	SXE_ME(children,               arginfo_simplexmlelement_children, ZEND_ACC_PUBLIC)
2588 2599
 	SXE_ME(getNamespaces,          arginfo_simplexmlelement_getnamespaces, ZEND_ACC_PUBLIC)
2589  
-	SXE_ME(getDocNamespaces,       arginfo_simplexmlelement_getnamespaces, ZEND_ACC_PUBLIC)
  2600
+	SXE_ME(getDocNamespaces,       arginfo_simplexmlelement_getdocnamespaces, ZEND_ACC_PUBLIC)
2590 2601
 	SXE_ME(getName,                arginfo_simplexmlelement__void, ZEND_ACC_PUBLIC)
2591 2602
 	SXE_ME(addChild,               arginfo_simplexmlelement_addchild, ZEND_ACC_PUBLIC)
2592 2603
 	SXE_ME(addAttribute,           arginfo_simplexmlelement_addchild, ZEND_ACC_PUBLIC)
117  ext/simplexml/tests/feature55218.phpt
... ...
@@ -0,0 +1,117 @@
  1
+--TEST--
  2
+Bug #55218 getDocNamespaces from current element and not root
  3
+--SKIPIF--
  4
+<?php
  5
+if (!extension_loaded("simplexml")) print "skip SimpleXML not present";
  6
+if (!extension_loaded("libxml")) print "skip LibXML not present";
  7
+?>
  8
+--FILE--
  9
+<?php 
  10
+
  11
+$x = new SimpleXMLElement(
  12
+'<?xml version="1.0" standalone="yes"?>
  13
+<people xmlns:p="http://example.org/p" >
  14
+    <person id="1" xmlns:t="http://example.org/t" >
  15
+                <t:name>John Doe</t:name>
  16
+        </person>
  17
+    <person id="2">Susie Q. Public</person>
  18
+    <o>
  19
+                <p:div>jdslkfjsldk jskdfjsmlkjfkldjkjflskj kljfslkjf sldk</p:div>
  20
+        </o>
  21
+</people>');
  22
+
  23
+echo "getDocNamespaces\n";
  24
+echo "\nBackwards Compatibility:\n";
  25
+echo "recursion:\n";
  26
+
  27
+var_dump ( $x->getDocNamespaces(true) ) ;
  28
+var_dump( $x->person[0]->getDocNamespaces(true) );
  29
+var_dump( $x->person[1]->getDocNamespaces(true) );
  30
+
  31
+echo "\nnon recursive:\n";
  32
+
  33
+var_dump( $x->getDocNamespaces(false) );
  34
+var_dump( $x->person[0]->getDocNamespaces(false) );
  35
+var_dump( $x->person[1]->getDocNamespaces(false) );
  36
+
  37
+echo "\n\nUsing new 'from_root' bool set to false:\n";
  38
+echo "recursion:\n";
  39
+
  40
+var_dump ( $x->getDocNamespaces(true, false) ) ;
  41
+var_dump( $x->person[0]->getDocNamespaces(true, false) );
  42
+var_dump( $x->person[1]->getDocNamespaces(true, false) );
  43
+
  44
+echo "\nnon recursive:\n";
  45
+
  46
+var_dump( $x->getDocNamespaces(false, false) );
  47
+var_dump( $x->person[0]->getDocNamespaces(false, false) );
  48
+var_dump( $x->person[1]->getDocNamespaces(false, false) );
  49
+
  50
+?>
  51
+===DONE===
  52
+--EXPECTF--
  53
+getDocNamespaces
  54
+
  55
+Backwards Compatibility:
  56
+recursion:
  57
+array(2) {
  58
+  ["p"]=>
  59
+  string(20) "http://example.org/p"
  60
+  ["t"]=>
  61
+  string(20) "http://example.org/t"
  62
+}
  63
+array(2) {
  64
+  ["p"]=>
  65
+  string(20) "http://example.org/p"
  66
+  ["t"]=>
  67
+  string(20) "http://example.org/t"
  68
+}
  69
+array(2) {
  70
+  ["p"]=>
  71
+  string(20) "http://example.org/p"
  72
+  ["t"]=>
  73
+  string(20) "http://example.org/t"
  74
+}
  75
+
  76
+non recursive:
  77
+array(1) {
  78
+  ["p"]=>
  79
+  string(20) "http://example.org/p"
  80
+}
  81
+array(1) {
  82
+  ["p"]=>
  83
+  string(20) "http://example.org/p"
  84
+}
  85
+array(1) {
  86
+  ["p"]=>
  87
+  string(20) "http://example.org/p"
  88
+}
  89
+
  90
+
  91
+Using new 'from_root' bool set to false:
  92
+recursion:
  93
+array(2) {
  94
+  ["p"]=>
  95
+  string(20) "http://example.org/p"
  96
+  ["t"]=>
  97
+  string(20) "http://example.org/t"
  98
+}
  99
+array(1) {
  100
+  ["t"]=>
  101
+  string(20) "http://example.org/t"
  102
+}
  103
+array(0) {
  104
+}
  105
+
  106
+non recursive:
  107
+array(1) {
  108
+  ["p"]=>
  109
+  string(20) "http://example.org/p"
  110
+}
  111
+array(1) {
  112
+  ["t"]=>
  113
+  string(20) "http://example.org/t"
  114
+}
  115
+array(0) {
  116
+}
  117
+===DONE===
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.