Hi Marcus,

I think it would be better to speficy namespace URI rather than prefix. Prefix can be anything, so unless you know what was used, you need to use the new namespace methods (which are primarily debug functions). It is more common to known the URI than prefix.

I havent tried the changes yet, but is it required to specify the namespace for a document element in a namespace or when loading does it automatically set the namespace scope based on the namespace of the doc element?

Rob

Marcus Boerger wrote:

helly           Fri Apr 14 14:07:51 2006 UTC

Added files: /php-src/ext/simplexml/tests profile12.phpt Modified files: /php-src/ext/simplexml simplexml.c Log:
 - Allow access to namespaced root by specifying prefix on creation
 - Fix access to non namespaced root
http://cvs.php.net/viewcvs.cgi/php-src/ext/simplexml/simplexml.c?r1=1.204&r2=1.205&diff_format=u
Index: php-src/ext/simplexml/simplexml.c
diff -u php-src/ext/simplexml/simplexml.c:1.204 
php-src/ext/simplexml/simplexml.c:1.205
--- php-src/ext/simplexml/simplexml.c:1.204     Fri Apr 14 12:18:15 2006
+++ php-src/ext/simplexml/simplexml.c   Fri Apr 14 14:07:51 2006
@@ -18,7 +18,7 @@
  +----------------------------------------------------------------------+
*/

-/* $Id: simplexml.c,v 1.204 2006/04/14 12:18:15 helly Exp $ */
+/* $Id: simplexml.c,v 1.205 2006/04/14 14:07:51 helly Exp $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -69,7 +69,7 @@
        if (name) {
                subnode->iter.name = xmlStrdup(name);
        }
-       if (prefix) {
+       if (prefix && *prefix) {
                subnode->iter.nsprefix = xmlStrdup(prefix);
        }

@@ -122,7 +122,7 @@
                return 1;
        }

-       if (node->ns && !xmlStrcmp(node->ns->href, name)) {
+       if (node->ns && (/*!xmlStrcmp(node->ns->prefix, name) ||*/ 
!xmlStrcmp(node->ns->href, name))) {
                return 1;
        }

@@ -1816,7 +1816,7 @@
}
/* }}} */

-/* {{{ proto simplemxml_element simplexml_load_file(string filename [, string 
class_name [, int options]])
+/* {{{ proto simplemxml_element simplexml_load_file(string filename [, string 
class_name [, int options [, string ns]]])
   Load a filename and return a simplexml_element object to allow for 
processing */
PHP_FUNCTION(simplexml_load_file)
{
@@ -1824,12 +1824,12 @@
        char           *filename;
        int             filename_len;
        xmlDocPtr       docp;
-       char           *classname = "";
-       int             classname_len = 0;
+       char           *classname = NULL, *ns = NULL;
+       int             classname_len = 0, ns_len = 0;
        long            options = 0;
        zend_class_entry *ce= sxe_class_entry;

-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sl", &filename, 
&filename_len, &classname, &classname_len, &options) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sls", &filename, &filename_len, 
&classname, &classname_len, &options, &ns, &ns_len) == FAILURE) {
                return;
        }

@@ -1848,6 +1848,7 @@
        }

        sxe = php_sxe_object_new(ce TSRMLS_CC);
+       sxe->iter.nsprefix = ns_len ? xmlStrdup(ns) : NULL;
        php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp 
TSRMLS_CC);
        php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, 
xmlDocGetRootElement(docp), NULL TSRMLS_CC);

@@ -1856,7 +1857,7 @@
}
/* }}} */

-/* {{{ proto simplemxml_element simplexml_load_string(string data [, string 
class_name [, int options]])
+/* {{{ proto simplemxml_element simplexml_load_string(string data [, string 
class_name [, int options [, string ns]]])
   Load a string and return a simplexml_element object to allow for processing 
*/
PHP_FUNCTION(simplexml_load_string)
{
@@ -1864,12 +1865,12 @@
        char           *data;
        int             data_len;
        xmlDocPtr       docp;
-       char           *classname = "";
-       int             classname_len = 0;
+       char           *classname = NULL, *ns = NULL;
+       int             classname_len = 0, ns_len = 0;
        long            options = 0;
        zend_class_entry *ce= sxe_class_entry;

-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sl", &data, &data_len, 
&classname, &classname_len, &options) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sls", &data, &data_len, 
&classname, &classname_len, &options, &ns, &ns_len) == FAILURE) {
                return;
        }

@@ -1888,6 +1889,7 @@
        }

        sxe = php_sxe_object_new(ce TSRMLS_CC);
+       sxe->iter.nsprefix = ns_len ? xmlStrdup(ns) : NULL;
        php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp 
TSRMLS_CC);
        php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, 
xmlDocGetRootElement(docp), NULL TSRMLS_CC);

@@ -1897,19 +1899,19 @@
/* }}} */


-/* {{{ proto SimpleXMLElement::__construct(string data [, int options [, bool 
data_is_url]])
+/* {{{ proto SimpleXMLElement::__construct(string data [, int options [, bool 
data_is_url [, string ns]]])
   SimpleXMLElement constructor */
SXE_METHOD(__construct)
{
        php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
-       char           *data;
-       int             data_len;
+       char           *data, *ns = NULL;
+       int             data_len, *ns_len = 0;
        xmlDocPtr       docp;
        long            options = 0;
        zend_bool       is_url = 0;

        php_set_error_handling(EH_THROW, zend_exception_get_default(TSRMLS_C) 
TSRMLS_CC);
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &data, &data_len, 
&options, &is_url) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lbs", &data, &data_len, 
&options, &is_url, &ns, &ns_len) == FAILURE) {
                php_std_error_handling();
                return;
        }
@@ -1924,6 +1926,7 @@
                return;
        }

+       sxe->iter.nsprefix = ns_len ? xmlStrdup(ns) : NULL;
        php_libxml_increment_doc_ref((php_libxml_node_object *)sxe, docp 
TSRMLS_CC);
        php_libxml_increment_node_ptr((php_libxml_node_object *)sxe, 
xmlDocGetRootElement(docp), NULL TSRMLS_CC);
}
@@ -2264,7 +2267,7 @@
{
        php_info_print_table_start();
        php_info_print_table_header(2, "Simplexml support", "enabled");
-       php_info_print_table_row(2, "Revision", "$Revision: 1.204 $");
+       php_info_print_table_row(2, "Revision", "$Revision: 1.205 $");
        php_info_print_table_row(2, "Schema support",
#ifdef LIBXML_SCHEMAS_ENABLED
                "enabled");

http://cvs.php.net/viewcvs.cgi/php-src/ext/simplexml/tests/profile12.phpt?view=markup&rev=1.1
Index: php-src/ext/simplexml/tests/profile12.phpt
+++ php-src/ext/simplexml/tests/profile12.phpt
--TEST--
SimpleXML [profile]: Accessing namespaced root and non namespaced children
--FILE--
<?php

$xml =<<<EOF
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema";
<soap:Body>
<businessList foo="bar">
<businessInfo businessKey="bla"/>
</businessList>
</soap:Body> </soap:Envelope>
EOF;

$sxe = simplexml_load_string($xml, NULL, 0, 'soap');
$nsl = $sxe->getNamespaces();
var_dump($nsl);

$sxe = simplexml_load_string($xml, NULL, 0, $nsl['soap']);
var_dump($sxe->Body);
var_dump($sxe->Body->children(''));
var_dump($sxe->Body->children('')->businessList);

?>
===DONE===
--EXPECTF--
array(1) {
 ["soap"]=>
 string(41) "http://schemas.xmlsoap.org/soap/envelope/";
}
object(SimpleXMLElement)#%d (0) {
}
object(SimpleXMLElement)#%d (1) {
 ["businessInfo"]=>
 object(SimpleXMLElement)#%d (1) {
   ["@attributes"]=>
   array(1) {
     ["businessKey"]=>
     string(3) "bla"
   }
 }
}
object(SimpleXMLElement)#%d (2) {
 ["@attributes"]=>
 array(1) {
   ["foo"]=>
   string(3) "bar"
 }
 ["businessInfo"]=>
 object(SimpleXMLElement)#%d (1) {
   ["@attributes"]=>
   array(1) {
     ["businessKey"]=>
     string(3) "bla"
   }
 }
}
===DONE===
--UEXPECTF--
array(1) {
 [u"soap"]=>
 string(41) "http://schemas.xmlsoap.org/soap/envelope/";
}
object(SimpleXMLElement)#%d (0) {
}
object(SimpleXMLElement)#%d (1) {
 [u"businessInfo"]=>
 object(SimpleXMLElement)#%d (1) {
   [u"@attributes"]=>
   array(1) {
     [u"businessKey"]=>
     unicode(3) "bla"
   }
 }
}
object(SimpleXMLElement)#%d (2) {
 [u"@attributes"]=>
 array(1) {
   [u"foo"]=>
   unicode(3) "bar"
 }
 [u"businessInfo"]=>
 object(SimpleXMLElement)#%d (1) {
   [u"@attributes"]=>
   array(1) {
     [u"businessKey"]=>
     unicode(3) "bla"
   }
 }
}
===DONE===


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

Reply via email to