rrichards Tue Jan 13 16:28:42 2004 EDT Modified files: /php-src/ext/simplexml simplexml.c /php-src/ext/simplexml/tests 017.phpt Log: add support for $foo["a:bar"] fix xsearch to only return values for text,element and attribute nodes fix getChildren and return array remove most methods update test
Index: php-src/ext/simplexml/simplexml.c diff -u php-src/ext/simplexml/simplexml.c:1.103 php-src/ext/simplexml/simplexml.c:1.104 --- php-src/ext/simplexml/simplexml.c:1.103 Sat Jan 10 08:25:31 2004 +++ php-src/ext/simplexml/simplexml.c Tue Jan 13 16:28:40 2004 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: simplexml.c,v 1.103 2004/01/10 13:25:31 helly Exp $ */ +/* $Id: simplexml.c,v 1.104 2004/01/13 21:28:40 rrichards Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -158,9 +158,14 @@ if (node) { if (attribs) { + xmlChar *localname, *prefix=NULL; + localname = xmlSplitQName2(name, &prefix); + if (localname == NULL) { + localname = (xmlChar *)name; + } attr = node->properties; while (attr) { - if (!xmlStrcmp(attr->name, name)) { + if (!xmlStrcmp(attr->name, localname) && (prefix==NULL || (attr->ns && !xmlStrcmp(attr->ns->prefix, prefix)))) { APPEND_PREV_ELEMENT(counter, value); MAKE_STD_ZVAL(value); @@ -173,6 +178,12 @@ } attr = attr->next; } + if (prefix) { + xmlFree(prefix); + if (localname) { + xmlFree(localname); + } + } } if (elements) { @@ -319,9 +330,14 @@ if (node) { if (attribs) { + xmlChar *localname, *prefix=NULL; + localname = xmlSplitQName2(name, &prefix); + if (localname == NULL) { + localname = (xmlChar *)name; + } attr = node->properties; while (attr) { - if (!xmlStrcmp(attr->name, name)) { + if (!xmlStrcmp(attr->name, localname) && (prefix==NULL || (attr->ns && !xmlStrcmp(attr->ns->prefix, prefix)))) { is_attr = 1; ++counter; break; @@ -329,6 +345,12 @@ attr = attr->next; } + if (prefix) { + xmlFree(prefix); + if (localname) { + xmlFree(localname); + } + } } if (elements) { @@ -402,6 +424,7 @@ char *name; xmlNodePtr node; xmlAttrPtr attr = NULL; + int exists = 0; sxe = php_sxe_fetch_object(object TSRMLS_CC); name = Z_STRVAL_P(member); @@ -410,14 +433,26 @@ if (node) { if (attribs) { + xmlChar *localname, *prefix=NULL; + localname = xmlSplitQName2(name, &prefix); + if (localname == NULL) { + localname = (xmlChar *)name; + } attr = node->properties; while (attr) { - if (!xmlStrcmp(attr->name, name)) { - return 1; + if (!xmlStrcmp(attr->name, localname) && (prefix==NULL || (attr->ns && !xmlStrcmp(attr->ns->prefix, prefix)))) { + exists = 1; + break; } attr = attr->next; } + if (prefix) { + xmlFree(prefix); + if (localname) { + xmlFree(localname); + } + } } if (elements) { @@ -435,7 +470,7 @@ } } - return 0; + return exists; } /* }}} */ @@ -479,15 +514,27 @@ if (node) { if (attribs) { + + xmlChar *localname, *prefix=NULL; + localname = xmlSplitQName2(Z_STRVAL_P(member), &prefix); + if (localname == NULL) { + localname = (xmlChar *)Z_STRVAL_P(member); + } attr = node->properties; while (attr) { anext = attr->next; - if (!xmlStrcmp(attr->name, Z_STRVAL_P(member))) { + if (!xmlStrcmp(attr->name, localname) && (prefix==NULL || (attr->ns && !xmlStrcmp(attr->ns->prefix, prefix)))) { xmlUnlinkNode((xmlNodePtr) attr); php_libxml_node_free_resource((xmlNodePtr) attr TSRMLS_CC); } attr = anext; } + if (prefix) { + xmlFree(prefix); + if (localname) { + xmlFree(localname); + } + } } if (elements) { @@ -687,6 +734,7 @@ xmlNsPtr *ns = NULL; xmlXPathObjectPtr retval; xmlNodeSetPtr result; + xmlNodePtr nodeptr; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query, &query_len) == FAILURE) { return; @@ -740,18 +788,22 @@ array_init(return_value); for (i = 0; i < result->nodeNr; ++i) { - MAKE_STD_ZVAL(value); - /** - * Detect the case where the last selector is text(), simplexml - * always accesses the text() child by default, therefore we assign - * to the parent node. - */ - if (result->nodeTab[i]->type == XML_TEXT_NODE) { - _node_as_zval(sxe, result->nodeTab[i]->parent, value TSRMLS_CC); - } else { - _node_as_zval(sxe, result->nodeTab[i], value TSRMLS_CC); + nodeptr = result->nodeTab[i]; + if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE) { + MAKE_STD_ZVAL(value); + /** + * Detect the case where the last selector is text(), simplexml + * always accesses the text() child by default, therefore we assign + * to the parent node. + */ + if (nodeptr->type == XML_TEXT_NODE) { + _node_as_zval(sxe, nodeptr->parent, value TSRMLS_CC); + } else { + _node_as_zval(sxe, nodeptr, value TSRMLS_CC); + } + + add_next_index_zval(return_value, value); } - add_next_index_zval(return_value, value); } xmlXPathFreeObject(retval); @@ -912,31 +964,6 @@ } /* }}} */ -/* {{{ simplexml_count() - */ -SXE_METHOD(count) -{ - char *name; - int name_len; - HashTable *subnodes; - zval **data_ptr; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { - return; - } - subnodes = sxe_properties_get(getThis() TSRMLS_CC); - if (zend_hash_find(subnodes, name, name_len+1, (void **) &data_ptr) == SUCCESS) { - if (Z_TYPE_PP(data_ptr) == IS_ARRAY) { - RETURN_LONG(zend_hash_num_elements(Z_ARRVAL_PP(data_ptr))); - } else { - RETURN_LONG(1); - } - } else { - RETURN_LONG(0); - } -} -/* }}} */ - /* {{{ simplexml_attributes() */ SXE_METHOD(attributes) @@ -1307,14 +1334,6 @@ SKIP_TEXT(node); - do if (node->ns) { - if (node->parent->ns) { - if (!xmlStrcmp(node->ns->href, node->parent->ns->href)) { - break; - } - } - } while (0); - if (!sxe->iter.node->name) { goto next_iter; } else { @@ -1393,103 +1412,35 @@ php_sxe_iterator_current(iterator->sxe TSRMLS_CC); } -/* {{{ rewind() - */ -SXE_METHOD(rewind) -{ - php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); - GET_NODE(sxe, sxe->iter.node); - if (sxe->iter.node) { - sxe->iter.node = sxe->iter.node->children; - } - php_sxe_iterator_current(sxe TSRMLS_CC); -} -/* }}} */ - -/* {{{ hasMore() - */ -SXE_METHOD(hasMore) -{ - php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); - - RETURN_BOOL(sxe->iter.node); -} -/* }}} */ - -/* {{{ curent() - */ -SXE_METHOD(current) -{ - php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); - - RETURN_ZVAL(sxe->iter.data, 1, 0); -} -/* }}} */ - -/* {{{ key() - */ -SXE_METHOD(key) -{ - php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); - - RETURN_STRINGL(sxe->iter.name, sxe->iter.namelen-1, 1); -} -/* }}} */ - -/* {{{ next() +/* {{{ getChildren() */ -SXE_METHOD(next) +SXE_METHOD(getChildren) { - php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + php_sxe_object *sxe; + xmlNodePtr node; + xmlNodePtr child; + zval *value = NULL; - if (sxe->iter.node) { - sxe->iter.node = sxe->iter.node->next; + if (ZEND_NUM_ARGS() != 0) { + RETURN_FALSE; } - php_sxe_iterator_current(sxe TSRMLS_CC); -} -/* }}} */ -/* {{{ hasChildren() - */ -SXE_METHOD(hasChildren) -{ - php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); - php_sxe_object *child = php_sxe_fetch_object(sxe->iter.data TSRMLS_CC); - xmlNodePtr node; + sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); + GET_NODE(sxe, node); - GET_NODE(child, node); + array_init(return_value); if (node) { - node = node->children; - } - while (node) { - SKIP_TEXT(node); - - do if (node->ns) { - if (node->parent->ns) { - if (!xmlStrcmp(node->ns->href, node->parent->ns->href)) { - break; - } + child = node->children; + while (child) { + if (node->type == XML_ELEMENT_NODE) { + MAKE_STD_ZVAL(value); + _node_as_zval(sxe, child, value TSRMLS_CC); + add_next_index_zval(return_value, value); } - } while (0); - if (node->type == XML_ELEMENT_NODE) { - break; + child = child->next; } -next_iter: - node = node->next; } - RETURN_BOOL(node ? 1 : 0); -} -/* }}} */ - -/* {{{ getChildren() - */ -SXE_METHOD(getChildren) -{ - php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC); - - return_value->type = IS_OBJECT; - return_value->value.obj = zend_objects_store_clone_obj(sxe->iter.data TSRMLS_CC); } /* }}} */ @@ -1582,14 +1533,7 @@ SXE_ME(validate_schema_file, NULL, ZEND_ACC_PUBLIC) #endif SXE_ME(xsearch, NULL, ZEND_ACC_PUBLIC) - SXE_ME(rewind, NULL, ZEND_ACC_PUBLIC) - SXE_ME(hasMore, NULL, ZEND_ACC_PUBLIC) - SXE_ME(current, NULL, ZEND_ACC_PUBLIC) - SXE_ME(key, NULL, ZEND_ACC_PUBLIC) - SXE_ME(next, NULL, ZEND_ACC_PUBLIC) - SXE_ME(hasChildren, NULL, ZEND_ACC_PUBLIC) SXE_ME(getChildren, NULL, ZEND_ACC_PUBLIC) - SXE_ME(count, NULL, ZEND_ACC_PUBLIC) SXE_ME(attributes, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; @@ -1630,7 +1574,7 @@ { php_info_print_table_start(); php_info_print_table_header(2, "Simplexml support", "enabled"); - php_info_print_table_row(2, "Revision", "$Revision: 1.103 $"); + php_info_print_table_row(2, "Revision", "$Revision: 1.104 $"); php_info_print_table_row(2, "Schema support", #ifdef LIBXML_SCHEMAS_ENABLED "enabled"); Index: php-src/ext/simplexml/tests/017.phpt diff -u php-src/ext/simplexml/tests/017.phpt:1.1 php-src/ext/simplexml/tests/017.phpt:1.2 --- php-src/ext/simplexml/tests/017.phpt:1.1 Thu Dec 18 08:28:00 2003 +++ php-src/ext/simplexml/tests/017.phpt Tue Jan 13 16:28:41 2004 @@ -35,10 +35,10 @@ } function print_xml2($xml) { - $persons = $xml->count("person"); + $persons = count($xml->person); for ($i=0;$i<$persons;$i++) { echo "person: ".$xml->person[$i]['name']."\n"; - $children = $xml->person[$i]->count("child"); + $children = count($xml->person[$i]->child); for ($j=0;$j<$children;$j++) { echo " child: ".$xml->person[$i]->child[$j]['name']."\n"; }
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php