From c3b47f5aa48c1a0648b43b14f1f4523c63a02400 Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Fri, 14 Apr 2006 15:15:21 +0000 Subject: [PATCH] - Fix properties access to children() result - Allow access by namespace prefix --- ext/simplexml/php_simplexml.h | 1 + ext/simplexml/simplexml.c | 112 +++++++++++++++------------- ext/simplexml/tests/009b.phpt | 48 ++++++++++++ ext/simplexml/tests/profile12.phpt | 48 ++++++++---- ext/simplexml/tests/profile13.phpt | 115 +++++++++++++++++++++++++++++ 5 files changed, 257 insertions(+), 67 deletions(-) create mode 100755 ext/simplexml/tests/009b.phpt create mode 100755 ext/simplexml/tests/profile13.phpt diff --git a/ext/simplexml/php_simplexml.h b/ext/simplexml/php_simplexml.h index 5af5e67245..946e7a160d 100644 --- a/ext/simplexml/php_simplexml.h +++ b/ext/simplexml/php_simplexml.h @@ -69,6 +69,7 @@ typedef struct { struct { char *name; char *nsprefix; + int isprefix; SXE_ITER type; zval *data; } iter; diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index 6651f7a296..68c6cbe184 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -58,7 +58,7 @@ static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, i /* {{{ _node_as_zval() */ -static void _node_as_zval(php_sxe_object *sxe, xmlNodePtr node, zval *value, SXE_ITER itertype, char *name, char *prefix TSRMLS_DC) +static void _node_as_zval(php_sxe_object *sxe, xmlNodePtr node, zval *value, SXE_ITER itertype, char *name, char *nsprefix, int isprefix TSRMLS_DC) { php_sxe_object *subnode; @@ -69,8 +69,9 @@ static void _node_as_zval(php_sxe_object *sxe, xmlNodePtr node, zval *value, SXE if (name) { subnode->iter.name = xmlStrdup(name); } - if (prefix && *prefix) { - subnode->iter.nsprefix = xmlStrdup(prefix); + if (nsprefix && *nsprefix) { + subnode->iter.nsprefix = xmlStrdup(nsprefix); + subnode->iter.isprefix = isprefix; } php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL TSRMLS_CC); @@ -116,13 +117,13 @@ static xmlNodePtr php_sxe_get_first_node(php_sxe_object *sxe, xmlNodePtr node TS } } -static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name) /* {{{ */ +static inline int match_ns(php_sxe_object *sxe, xmlNodePtr node, xmlChar *name, int prefix) /* {{{ */ { if (name == NULL && (node->ns == NULL || node->ns->prefix == NULL)) { return 1; } - if (node->ns && (/*!xmlStrcmp(node->ns->prefix, name) ||*/ !xmlStrcmp(node->ns->href, name))) { + if (node->ns && !xmlStrcmp(prefix ? node->ns->prefix : node->ns->href, name)) { return 1; } @@ -139,7 +140,7 @@ static xmlNodePtr sxe_get_element_by_offset(php_sxe_object *sxe, long offset, xm } while (node && nodendx <= offset) { SKIP_TEXT(node) - if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix)) { + if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) { if (sxe->iter.type == SXE_ITER_CHILD || ( sxe->iter.type == SXE_ITER_ELEMENT && !xmlStrcmp(node->name, sxe->iter.name))) { if (nodendx == offset) { @@ -164,7 +165,7 @@ static xmlNodePtr sxe_find_element_by_name(php_sxe_object *sxe, xmlNodePtr node, { while (node) { SKIP_TEXT(node) - if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix)) { + if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) { if (!xmlStrcmp(node->name, name)) { return node; } @@ -198,7 +199,7 @@ static xmlNodePtr sxe_get_element_by_name(php_sxe_object *sxe, xmlNodePtr node, while (node) { SKIP_TEXT(node) - if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix)) { + if (node->type == XML_ELEMENT_NODE && match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix)) { if (!xmlStrcmp(node->name, *name)) { if (1||retnode) { @@ -276,9 +277,9 @@ static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, if (Z_TYPE_P(member) != IS_LONG || sxe->iter.type == SXE_ITER_ATTRLIST) { if (Z_TYPE_P(member) == IS_LONG) { while (attr && nodendx <= Z_LVAL_P(member)) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { if (nodendx == Z_LVAL_P(member)) { - _node_as_zval(sxe, (xmlNodePtr) attr, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix TSRMLS_CC); + _node_as_zval(sxe, (xmlNodePtr) attr, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); break; } nodendx++; @@ -287,8 +288,8 @@ static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, } } else { while (attr) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { - _node_as_zval(sxe, (xmlNodePtr) attr, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix TSRMLS_CC); + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { + _node_as_zval(sxe, (xmlNodePtr) attr, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); break; } attr = attr->next; @@ -307,7 +308,7 @@ static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, } node = sxe_get_element_by_offset(sxe, Z_LVAL_P(member), node, NULL); if (node) { - _node_as_zval(sxe, node, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix TSRMLS_CC); + _node_as_zval(sxe, node, return_value, SXE_ITER_NONE, NULL, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); } } else { #if SXE_ELEMENT_BY_NAME @@ -316,10 +317,10 @@ static zval * sxe_prop_dim_read(zval *object, zval *member, zend_bool elements, GET_NODE(sxe, node); node = sxe_get_element_by_name(sxe, node, &name, &newtype TSRMLS_CC); if (node) { - _node_as_zval(sxe, node, return_value, newtype, name, sxe->iter.nsprefix TSRMLS_CC); + _node_as_zval(sxe, node, return_value, newtype, name, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); } #else - _node_as_zval(sxe, node, return_value, SXE_ITER_ELEMENT, name, sxe->iter.nsprefix TSRMLS_CC); + _node_as_zval(sxe, node, return_value, SXE_ITER_ELEMENT, name, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); #endif } } @@ -495,7 +496,7 @@ static void sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_boo if (attribs) { if (Z_TYPE_P(member) == IS_LONG) { while (attr && nodendx <= Z_LVAL_P(member)) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { if (nodendx == Z_LVAL_P(member)) { is_attr = 1; ++counter; @@ -507,7 +508,7 @@ static void sxe_prop_dim_write(zval *object, zval *member, zval *value, zend_boo } } else { while (attr) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, name) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { is_attr = 1; ++counter; break; @@ -615,7 +616,7 @@ static zval** sxe_property_get_adr(zval *object, zval *member TSRMLS_DC) /* {{{ name = NULL; } MAKE_STD_ZVAL(return_value); - _node_as_zval(sxe, node, return_value, type, name, sxe->iter.nsprefix TSRMLS_CC); + _node_as_zval(sxe, node, return_value, type, name, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); sxe = php_sxe_fetch_object(return_value TSRMLS_CC); sxe->tmp = return_value; @@ -675,7 +676,7 @@ static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend int nodendx = 0; while (attr && nodendx <= Z_LVAL_P(member)) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { if (nodendx == Z_LVAL_P(member)) { exists = 1; break; @@ -686,7 +687,7 @@ static int sxe_prop_dim_exists(zval *object, zval *member, int check_empty, zend } } else { while (attr) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { exists = 1; break; } @@ -795,7 +796,7 @@ static void sxe_prop_dim_delete(zval *object, zval *member, zend_bool elements, int nodendx = 0; while (attr && nodendx <= Z_LVAL_P(member)) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { if (nodendx == Z_LVAL_P(member)) { xmlUnlinkNode((xmlNodePtr) attr); php_libxml_node_free_resource((xmlNodePtr) attr TSRMLS_CC); @@ -808,7 +809,7 @@ static void sxe_prop_dim_delete(zval *object, zval *member, zend_bool elements, } else { while (attr) { anext = attr->next; - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && !xmlStrcmp(attr->name, Z_STRVAL_P(member)) && match_ns(sxe, (xmlNodePtr) attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { xmlUnlinkNode((xmlNodePtr) attr); php_libxml_node_free_resource((xmlNodePtr) attr TSRMLS_CC); break; @@ -871,7 +872,7 @@ static void sxe_dimension_delete(zval *object, zval *offset TSRMLS_DC) /* {{{ _get_base_node_value() */ -static void _get_base_node_value(php_sxe_object *sxe_ref, xmlNodePtr node, zval **value, char *prefix TSRMLS_DC) +static void _get_base_node_value(php_sxe_object *sxe_ref, xmlNodePtr node, zval **value, char *nsprefix, int isprefix TSRMLS_DC) { php_sxe_object *subnode; xmlChar *contents; @@ -888,8 +889,9 @@ static void _get_base_node_value(php_sxe_object *sxe_ref, xmlNodePtr node, zval subnode = php_sxe_object_new(sxe_ref->zo.ce TSRMLS_CC); subnode->document = sxe_ref->document; subnode->document->refcount++; - if (prefix) { - subnode->iter.nsprefix = xmlStrdup(prefix); + if (nsprefix && *nsprefix) { + subnode->iter.nsprefix = xmlStrdup(nsprefix); + subnode->iter.isprefix = isprefix; } php_libxml_increment_node_ptr((php_libxml_node_object *)subnode, node, NULL TSRMLS_CC); @@ -961,7 +963,7 @@ static HashTable * sxe_properties_get(zval *object TSRMLS_DC) zattr = NULL; test = sxe->iter.name && sxe->iter.type == SXE_ITER_ATTRLIST; while (attr) { - if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr)attr, sxe->iter.nsprefix)) { + if ((!test || !xmlStrcmp(attr->name, sxe->iter.name)) && match_ns(sxe, (xmlNodePtr)attr, sxe->iter.nsprefix, sxe->iter.isprefix)) { xmlChar *tmp; MAKE_STD_ZVAL(value); @@ -990,7 +992,7 @@ static HashTable * sxe_properties_get(zval *object TSRMLS_DC) xmlFree(tmp); zend_hash_next_index_insert(rv, &value, sizeof(zval *), NULL); node = NULL; - } else { + } else if (sxe->iter.type != SXE_ITER_CHILD) { node = node->children; } @@ -1009,7 +1011,7 @@ static HashTable * sxe_properties_get(zval *object TSRMLS_DC) } } - if (node->type == XML_ELEMENT_NODE && (! match_ns(sxe, node, sxe->iter.nsprefix))) { + if (node->type == XML_ELEMENT_NODE && (! match_ns(sxe, node, sxe->iter.nsprefix, sxe->iter.isprefix))) { goto next_iter; } @@ -1020,7 +1022,7 @@ static HashTable * sxe_properties_get(zval *object TSRMLS_DC) namelen = xmlStrlen(node->name) + 1; } - _get_base_node_value(sxe, node, &value, sxe->iter.nsprefix TSRMLS_CC); + _get_base_node_value(sxe, node, &value, sxe->iter.nsprefix, sxe->iter.isprefix TSRMLS_CC); sxe_properties_add(rv, name, namelen, value TSRMLS_CC); next_iter: @@ -1126,11 +1128,11 @@ SXE_METHOD(xpath) * to the parent node. */ if (nodeptr->type == XML_TEXT_NODE) { - _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_NONE, NULL, NULL TSRMLS_CC); + _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC); } else if (nodeptr->type == XML_ATTRIBUTE_NODE) { - _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, NULL TSRMLS_CC); + _node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, NULL, 0 TSRMLS_CC); } else { - _node_as_zval(sxe, nodeptr, value, SXE_ITER_NONE, NULL, NULL TSRMLS_CC); + _node_as_zval(sxe, nodeptr, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC); } add_next_index_zval(return_value, value); @@ -1344,7 +1346,7 @@ SXE_METHOD(getDocNamespaces) } /* }}} */ -/* {{{ proto object SimpleXMLElement::children([string ns]) +/* {{{ proto object SimpleXMLElement::children([string ns [, bool is_prefix]]) Finds children of given node */ SXE_METHOD(children) { @@ -1352,8 +1354,9 @@ SXE_METHOD(children) char *nsprefix = NULL; int nsprefix_len; xmlNodePtr node; + zend_bool isprefix = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &nsprefix, &nsprefix_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) { return; } @@ -1366,7 +1369,7 @@ SXE_METHOD(children) GET_NODE(sxe, node); node = php_sxe_get_first_node(sxe, node TSRMLS_CC); - _node_as_zval(sxe, node, return_value, SXE_ITER_CHILD, NULL, nsprefix TSRMLS_CC); + _node_as_zval(sxe, node, return_value, SXE_ITER_CHILD, NULL, nsprefix, isprefix TSRMLS_CC); } /* }}} */ @@ -1388,7 +1391,7 @@ SXE_METHOD(getName) } /* }}} */ -/* {{{ proto array SimpleXMLElement::attributes([string ns]) +/* {{{ proto array SimpleXMLElement::attributes([string ns [, bool is_prefix]]) Identifies an element's attributes */ SXE_METHOD(attributes) { @@ -1396,8 +1399,9 @@ SXE_METHOD(attributes) char *nsprefix = NULL; int nsprefix_len; xmlNodePtr node; + zend_bool isprefix = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &nsprefix, &nsprefix_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &nsprefix, &nsprefix_len, &isprefix) == FAILURE) { return; } @@ -1410,11 +1414,11 @@ SXE_METHOD(attributes) node = php_sxe_get_first_node(sxe, node TSRMLS_CC); - _node_as_zval(sxe, node, return_value, SXE_ITER_ATTRLIST, NULL, nsprefix TSRMLS_CC); + _node_as_zval(sxe, node, return_value, SXE_ITER_ATTRLIST, NULL, nsprefix, isprefix TSRMLS_CC); } /* }}} */ -/* {{{ proto void SimpleXMLElement::addChild(string qName [, string value [,string ns]]) +/* {{{ proto void SimpleXMLElement::addChild(string qName [, string value [, string ns]]) Add Element with optional namespace information */ SXE_METHOD(addChild) { @@ -1461,7 +1465,7 @@ SXE_METHOD(addChild) newnode->ns = nsptr; } - _node_as_zval(sxe, newnode, return_value, SXE_ITER_NONE, localname, prefix TSRMLS_CC); + _node_as_zval(sxe, newnode, return_value, SXE_ITER_NONE, localname, prefix, 0 TSRMLS_CC); xmlFree(localname); if (prefix != NULL) { @@ -1816,7 +1820,7 @@ sxe_object_new(zend_class_entry *ce TSRMLS_DC) } /* }}} */ -/* {{{ proto simplemxml_element simplexml_load_file(string filename [, string class_name [, int options [, string ns]]]) +/* {{{ proto simplemxml_element simplexml_load_file(string filename [, string class_name [, int options [, string ns [, bool is_prefix]]]]) Load a filename and return a simplexml_element object to allow for processing */ PHP_FUNCTION(simplexml_load_file) { @@ -1828,8 +1832,9 @@ PHP_FUNCTION(simplexml_load_file) int classname_len = 0, ns_len = 0; long options = 0; zend_class_entry *ce= sxe_class_entry; + zend_bool isprefix = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sls", &filename, &filename_len, &classname, &classname_len, &options, &ns, &ns_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slsb", &filename, &filename_len, &classname, &classname_len, &options, &ns, &ns_len, &isprefix) == FAILURE) { return; } @@ -1849,6 +1854,7 @@ PHP_FUNCTION(simplexml_load_file) sxe = php_sxe_object_new(ce TSRMLS_CC); sxe->iter.nsprefix = ns_len ? xmlStrdup(ns) : NULL; + sxe->iter.isprefix = isprefix; 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); @@ -1857,7 +1863,7 @@ PHP_FUNCTION(simplexml_load_file) } /* }}} */ -/* {{{ proto simplemxml_element simplexml_load_string(string data [, string class_name [, int options [, string ns]]]) +/* {{{ proto simplemxml_element simplexml_load_string(string data [, string class_name [, int options [, string ns [, bool is_prefix]]]]) Load a string and return a simplexml_element object to allow for processing */ PHP_FUNCTION(simplexml_load_string) { @@ -1869,8 +1875,9 @@ PHP_FUNCTION(simplexml_load_string) int classname_len = 0, ns_len = 0; long options = 0; zend_class_entry *ce= sxe_class_entry; + zend_bool isprefix = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sls", &data, &data_len, &classname, &classname_len, &options, &ns, &ns_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slsb", &data, &data_len, &classname, &classname_len, &options, &ns, &ns_len, &isprefix) == FAILURE) { return; } @@ -1890,6 +1897,7 @@ PHP_FUNCTION(simplexml_load_string) sxe = php_sxe_object_new(ce TSRMLS_CC); sxe->iter.nsprefix = ns_len ? xmlStrdup(ns) : NULL; + sxe->iter.isprefix = isprefix; 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); @@ -1899,7 +1907,7 @@ PHP_FUNCTION(simplexml_load_string) /* }}} */ -/* {{{ proto SimpleXMLElement::__construct(string data [, int options [, bool data_is_url [, string ns]]]) +/* {{{ proto SimpleXMLElement::__construct(string data [, int options [, bool data_is_url [, string ns [, bool is_prefix]]]]) SimpleXMLElement constructor */ SXE_METHOD(__construct) { @@ -1908,10 +1916,10 @@ SXE_METHOD(__construct) int data_len, *ns_len = 0; xmlDocPtr docp; long options = 0; - zend_bool is_url = 0; + zend_bool is_url = 0, isprefix = 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|lbs", &data, &data_len, &options, &is_url, &ns, &ns_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lbsb", &data, &data_len, &options, &is_url, &ns, &ns_len, &isprefix) == FAILURE) { php_std_error_handling(); return; } @@ -1927,6 +1935,7 @@ SXE_METHOD(__construct) } sxe->iter.nsprefix = ns_len ? xmlStrdup(ns) : NULL; + sxe->iter.isprefix = isprefix; 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); } @@ -1951,18 +1960,19 @@ zend_object_iterator_funcs php_sxe_iterator_funcs = { static xmlNodePtr php_sxe_iterator_fetch(php_sxe_object *sxe, xmlNodePtr node, int use_data TSRMLS_DC) { - char *prefix = sxe->iter.nsprefix; + char *prefix = sxe->iter.nsprefix; + int isprefix = sxe->iter.isprefix; int test_elem = sxe->iter.type == SXE_ITER_ELEMENT && sxe->iter.name; int test_attr = sxe->iter.type == SXE_ITER_ATTRLIST && sxe->iter.name; while (node) { SKIP_TEXT(node); if (sxe->iter.type != SXE_ITER_ATTRLIST && node->type == XML_ELEMENT_NODE) { - if ((!test_elem || !xmlStrcmp(node->name, sxe->iter.name)) && match_ns(sxe, node, prefix)) { + if ((!test_elem || !xmlStrcmp(node->name, sxe->iter.name)) && match_ns(sxe, node, prefix, isprefix)) { break; } } else if (node->type == XML_ATTRIBUTE_NODE) { - if ((!test_attr || !xmlStrcmp(node->name, sxe->iter.name)) && match_ns(sxe, node, prefix)) { + if ((!test_attr || !xmlStrcmp(node->name, sxe->iter.name)) && match_ns(sxe, node, prefix, isprefix)) { break; } } @@ -1972,7 +1982,7 @@ next_iter: if (node && use_data) { ALLOC_INIT_ZVAL(sxe->iter.data); - _node_as_zval(sxe, node, sxe->iter.data, SXE_ITER_NONE, NULL, sxe->iter.nsprefix TSRMLS_CC); + _node_as_zval(sxe, node, sxe->iter.data, SXE_ITER_NONE, NULL, prefix, isprefix TSRMLS_CC); } return node; diff --git a/ext/simplexml/tests/009b.phpt b/ext/simplexml/tests/009b.phpt new file mode 100755 index 0000000000..3695d34dfc --- /dev/null +++ b/ext/simplexml/tests/009b.phpt @@ -0,0 +1,48 @@ +--TEST-- +SimpleXML: foreach +--SKIPIF-- + +--FILE-- + + + + Plain text. + Bla bla 1. + Here we have some text data. + + Bla bla 2. + +EOF +); +var_dump($sxe->children()); +?> +===DONE=== + +--EXPECTF-- +object(SimpleXMLElement)#%d (3) { + ["@attributes"]=> + array(1) { + ["id"]=> + string(5) "elem1" + } + ["elem1"]=> + string(10) "Bla bla 1." + ["elem11"]=> + string(10) "Bla bla 2." +} +===DONE=== +--UEXPECTF-- +object(SimpleXMLElement)#%d (3) { + [u"@attributes"]=> + array(1) { + [u"id"]=> + unicode(5) "elem1" + } + [u"elem1"]=> + unicode(10) "Bla bla 1." + [u"elem11"]=> + unicode(10) "Bla bla 2." +} +===DONE=== diff --git a/ext/simplexml/tests/profile12.phpt b/ext/simplexml/tests/profile12.phpt index 96c2fa6747..b965a9a47d 100755 --- a/ext/simplexml/tests/profile12.phpt +++ b/ext/simplexml/tests/profile12.phpt @@ -34,26 +34,34 @@ array(1) { ["soap"]=> string(41) "http://schemas.xmlsoap.org/soap/envelope/" } -object(SimpleXMLElement)#%d (0) { +object(SimpleXMLElement)#%s (0) { } -object(SimpleXMLElement)#%d (1) { - ["businessInfo"]=> - object(SimpleXMLElement)#%d (1) { +object(SimpleXMLElement)#%s (1) { + ["businessList"]=> + object(SimpleXMLElement)#%s (2) { ["@attributes"]=> array(1) { - ["businessKey"]=> - string(3) "bla" + ["foo"]=> + string(3) "bar" + } + ["businessInfo"]=> + object(SimpleXMLElement)#%s (1) { + ["@attributes"]=> + array(1) { + ["businessKey"]=> + string(3) "bla" + } } } } -object(SimpleXMLElement)#%d (2) { +object(SimpleXMLElement)#%s (2) { ["@attributes"]=> array(1) { ["foo"]=> string(3) "bar" } ["businessInfo"]=> - object(SimpleXMLElement)#%d (1) { + object(SimpleXMLElement)#%s (1) { ["@attributes"]=> array(1) { ["businessKey"]=> @@ -67,26 +75,34 @@ array(1) { [u"soap"]=> string(41) "http://schemas.xmlsoap.org/soap/envelope/" } -object(SimpleXMLElement)#%d (0) { +object(SimpleXMLElement)#%s (0) { } -object(SimpleXMLElement)#%d (1) { - [u"businessInfo"]=> - object(SimpleXMLElement)#%d (1) { +object(SimpleXMLElement)#%s (1) { + [u"businessList"]=> + object(SimpleXMLElement)#%s (2) { [u"@attributes"]=> array(1) { - [u"businessKey"]=> - unicode(3) "bla" + [u"foo"]=> + unicode(3) "bar" + } + [u"businessInfo"]=> + object(SimpleXMLElement)#%s (1) { + [u"@attributes"]=> + array(1) { + [u"businessKey"]=> + unicode(3) "bla" + } } } } -object(SimpleXMLElement)#%d (2) { +object(SimpleXMLElement)#%s (2) { [u"@attributes"]=> array(1) { [u"foo"]=> unicode(3) "bar" } [u"businessInfo"]=> - object(SimpleXMLElement)#%d (1) { + object(SimpleXMLElement)#%s (1) { [u"@attributes"]=> array(1) { [u"businessKey"]=> diff --git a/ext/simplexml/tests/profile13.phpt b/ext/simplexml/tests/profile13.phpt new file mode 100755 index 0000000000..b7a1caa5bf --- /dev/null +++ b/ext/simplexml/tests/profile13.phpt @@ -0,0 +1,115 @@ +--TEST-- +SimpleXML [profile]: Accessing by namespace prefix +--FILE-- + + + + + + + + +EOF; + +$sxe = simplexml_load_string($xml); +var_dump($sxe->children('soap', 1)); + +$sxe = simplexml_load_string($xml, NULL, 0, 'soap', 1); +var_dump($sxe->Body); +var_dump($sxe->Body->children('')); +var_dump($sxe->Body->children('')->businessList); + +?> +===DONE=== + +--EXPECTF-- +object(SimpleXMLElement)#%d (1) { + ["Body"]=> + object(SimpleXMLElement)#%d (0) { + } +} +object(SimpleXMLElement)#%d (0) { +} +object(SimpleXMLElement)#%d (1) { + ["businessList"]=> + object(SimpleXMLElement)#%d (2) { + ["@attributes"]=> + array(1) { + ["foo"]=> + string(3) "bar" + } + ["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-- +object(SimpleXMLElement)#%d (1) { + [u"Body"]=> + object(SimpleXMLElement)#%d (0) { + } +} +object(SimpleXMLElement)#%d (0) { +} +object(SimpleXMLElement)#%d (1) { + [u"businessList"]=> + 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" + } + } + } +} +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=== -- 2.50.1