helly           Mon Oct 31 14:09:13 2005 EDT

  Added files:                 
    /php-src/ext/simplexml/tests        025.phpt 

  Modified files:              
    /php-src/ext/simplexml      simplexml.c 
  Log:
  - Add functions to check for registered/in use namespaces
  
  
http://cvs.php.net/diff.php/php-src/ext/simplexml/simplexml.c?r1=1.170&r2=1.171&ty=u
Index: php-src/ext/simplexml/simplexml.c
diff -u php-src/ext/simplexml/simplexml.c:1.170 
php-src/ext/simplexml/simplexml.c:1.171
--- php-src/ext/simplexml/simplexml.c:1.170     Sun Oct 30 15:37:07 2005
+++ php-src/ext/simplexml/simplexml.c   Mon Oct 31 14:09:10 2005
@@ -18,7 +18,7 @@
   +----------------------------------------------------------------------+
 */
 
-/* $Id: simplexml.c,v 1.170 2005/10/30 20:37:07 helly Exp $ */
+/* $Id: simplexml.c,v 1.171 2005/10/31 19:09:10 helly Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -896,10 +896,7 @@
 }
 /* }}} */
 
-/* {{{ sxe_objects_compare()
- */
-static int
-sxe_objects_compare(zval *object1, zval *object2 TSRMLS_DC)
+static int sxe_objects_compare(zval *object1, zval *object2 TSRMLS_DC) /* {{{ 
*/
 {
        php_sxe_object *sxe1;
        php_sxe_object *sxe2;
@@ -1103,6 +1100,99 @@
 }
 /* }}} */
 
+static void sxe_add_namespaces(php_sxe_object *sxe, xmlNodePtr node, zend_bool 
recursive, zval *return_value TSRMLS_DC) /* {{{ */
+{
+       xmlAttrPtr  attr;
+
+       if (node->ns) { 
+               add_assoc_string(return_value, (char*)node->ns->prefix, 
(char*)node->ns->href, 1);
+       }
+
+       attr = node->properties;
+       while (attr) {
+               if (attr->ns) { 
+                       add_assoc_string(return_value, (char*)attr->ns->prefix, 
(char*)attr->ns->href, 1);
+               }
+               attr = attr->next;
+       }
+
+       if (recursive) {
+               node = node->children;
+               while (node) {
+                       sxe_add_namespaces(sxe, node, recursive, return_value 
TSRMLS_CC);
+                       node = node->next;
+               }
+       }
+} /* }}} */
+
+/* {{{ proto string SimpleXMLElement::getNamespaces([bool recursve])
+   Return all namespaces in use */
+SXE_METHOD(getNamespaces)
+{
+       zend_bool           recursive = 0;
+       php_sxe_object     *sxe;
+       xmlNodePtr          node;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &recursive) 
== FAILURE) {
+               return;
+       }
+
+       array_init(return_value);
+
+       sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+       GET_NODE(sxe, node);
+       node = php_sxe_get_first_node(sxe, node TSRMLS_CC);
+
+       while (node) {
+               SKIP_TEXT(node)
+               if (node->type == XML_ELEMENT_NODE) {
+                       sxe_add_namespaces(sxe, node, recursive, return_value 
TSRMLS_CC);
+               } else if (node->ns) {
+                       add_assoc_string(return_value, (char*)node->ns->prefix, 
(char*)node->ns->href, 1);
+               }
+next_iter:
+               node = node->next;
+       }
+}
+/* }}} */
+
+static void sxe_add_registered_namespaces(php_sxe_object *sxe, xmlDocPtr doc, 
xmlNodePtr node, zend_bool recursive, zval *return_value TSRMLS_DC) /* {{{ */
+{
+       xmlNsPtr *ns = xmlGetNsList(doc, node);
+
+       while (ns && ns[0]) {
+               add_assoc_string(return_value, (char*)ns[0]->prefix, 
(char*)ns[0]->href, 1);
+               ns++;
+       }
+
+       if (recursive) {
+               node = node->children;
+               while (node) {
+                       sxe_add_registered_namespaces(sxe, doc, node, 
recursive, return_value TSRMLS_CC);
+                       node = node->next;
+               }
+       }
+}
+
+/* {{{ proto string SimpleXMLElement::getDocNamespaces([bool recursve])
+   Return all namespaces registered with document */
+SXE_METHOD(getDocNamespaces)
+{
+       zend_bool           recursive = 0;
+       php_sxe_object     *sxe;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &recursive) 
== FAILURE) {
+               return;
+       }
+
+       array_init(return_value);
+
+       sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
+
+       sxe_add_registered_namespaces(sxe, (xmlDocPtr)sxe->document->ptr, 
xmlDocGetRootElement((xmlDocPtr)sxe->document->ptr), recursive, return_value 
TSRMLS_CC);
+}
+/* }}} */
+
 /* {{{ proto object SimpleXMLElement::children()
    Finds children of given node */
 SXE_METHOD(children)
@@ -1204,7 +1294,7 @@
        int rv;
 
        sxe = php_sxe_fetch_object(readobj TSRMLS_CC);
-
+       
        if (type == IS_BOOL) {
                node = php_sxe_get_first_node(sxe, NULL TSRMLS_CC);
                INIT_PZVAL(writeobj);
@@ -1830,9 +1920,11 @@
        SXE_ME(__construct,            NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) /* 
must be called */
        SXE_ME(asXML,                  NULL, ZEND_ACC_PUBLIC)
        SXE_ME(xpath,                  NULL, ZEND_ACC_PUBLIC)
-       SXE_ME(registerXPathNamespace,      NULL, ZEND_ACC_PUBLIC)
+       SXE_ME(registerXPathNamespace, NULL, ZEND_ACC_PUBLIC)
        SXE_ME(attributes,             NULL, ZEND_ACC_PUBLIC)
-       SXE_ME(children,                           NULL, ZEND_ACC_PUBLIC)
+       SXE_ME(children,               NULL, ZEND_ACC_PUBLIC)
+       SXE_ME(getNamespaces,          NULL, ZEND_ACC_PUBLIC)
+       SXE_ME(getDocNamespaces,       NULL, ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };
 
@@ -1885,7 +1977,7 @@
 {
        php_info_print_table_start();
        php_info_print_table_header(2, "Simplexml support", "enabled");
-       php_info_print_table_row(2, "Revision", "$Revision: 1.170 $");
+       php_info_print_table_row(2, "Revision", "$Revision: 1.171 $");
        php_info_print_table_row(2, "Schema support",
 #ifdef LIBXML_SCHEMAS_ENABLED
                "enabled");

http://cvs.php.net/co.php/php-src/ext/simplexml/tests/025.phpt?r=1.1&p=1
Index: php-src/ext/simplexml/tests/025.phpt
+++ php-src/ext/simplexml/tests/025.phpt
--TEST--
SimpleXML: getting namespaces
--SKIPIF--
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
--FILE--
<?php

$xml =<<<EOF
<?xml version='1.0'?>
<xhtml:html xmlns:html='http://www.w3.org/1999/xhtml' 
xmlns:xhtml='http://www.w3.org/TR/REC-html40'>
<xhtml:head><xhtml:title>bla</xhtml:title></xhtml:head>
<xhtml:body html:title="b">
<html:h1>bla</html:h1>
<foo:bar xmlns:foo='foobar' xmlns:baz='foobarbaz'/>
</xhtml:body>
</xhtml:html>
EOF;

$sxe = simplexml_load_string($xml);

var_dump($sxe->getNamespaces());
var_dump($sxe->getNamespaces(true));
var_dump($sxe->getDocNamespaces());
var_dump($sxe->getDocNamespaces(true));

?>
===DONE===
--EXPECTF--
array(1) {
  ["xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
}
array(3) {
  ["xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
  ["html"]=>
  string(28) "http://www.w3.org/1999/xhtml";
  ["foo"]=>
  string(6) "foobar"
}
array(2) {
  ["html"]=>
  string(28) "http://www.w3.org/1999/xhtml";
  ["xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
}
array(4) {
  ["html"]=>
  string(28) "http://www.w3.org/1999/xhtml";
  ["xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
  ["foo"]=>
  string(6) "foobar"
  ["baz"]=>
  string(9) "foobarbaz"
}
===DONE===
--UEXPECTF--
array(1) {
  [u"xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
}
array(3) {
  [u"xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
  [u"html"]=>
  string(28) "http://www.w3.org/1999/xhtml";
  [u"foo"]=>
  string(6) "foobar"
}
array(2) {
  [u"html"]=>
  string(28) "http://www.w3.org/1999/xhtml";
  [u"xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
}
array(4) {
  [u"html"]=>
  string(28) "http://www.w3.org/1999/xhtml";
  [u"xhtml"]=>
  string(31) "http://www.w3.org/TR/REC-html40";
  [u"foo"]=>
  string(6) "foobar"
  [u"baz"]=>
  string(9) "foobarbaz"
}
===DONE===

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to