From: Marcus Boerger Date: Tue, 1 Nov 2005 11:53:14 +0000 (+0000) Subject: - Handle default namespaces X-Git-Tag: RELEASE_2_0_1~86 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=995674482190aaf150b97243503a5abe24ede7a8;p=php - Handle default namespaces # Another time it shows xml is far from being simple --- diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index a2030684fa..f9bc29d5dd 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -1102,18 +1102,25 @@ SXE_METHOD(asXML) } /* }}} */ +#define SXE_NS_PREFIX(ns) (ns->prefix ? (char*)ns->prefix : "") + +static inline void sxe_add_namespace_name(zval *return_value, xmlNsPtr ns) +{ + add_assoc_string(return_value, SXE_NS_PREFIX(ns), (char*)ns->href, 1); +} + 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); + sxe_add_namespace_name(return_value, node->ns); } attr = node->properties; while (attr) { if (attr->ns) { - add_assoc_string(return_value, (char*)attr->ns->prefix, (char*)attr->ns->href, 1); + sxe_add_namespace_name(return_value, attr->ns); } attr = attr->next; } @@ -1152,7 +1159,7 @@ SXE_METHOD(getNamespaces) if (node->type == XML_ELEMENT_NODE) { sxe_add_namespaces(sxe, node, recursive, return_value TSRMLS_CC); } else if (node->type == XML_ATTRIBUTE_NODE && node->ns) { - add_assoc_string(return_value, (char*)node->ns->prefix, (char*)node->ns->href, 1); + sxe_add_namespace_name(return_value, node->ns); } next_iter: node = node->next; @@ -1165,7 +1172,7 @@ static void sxe_add_registered_namespaces(php_sxe_object *sxe, xmlDocPtr doc, xm xmlNsPtr *ns = xmlGetNsList(doc, node); while (ns && ns[0]) { - add_assoc_string(return_value, (char*)ns[0]->prefix, (char*)ns[0]->href, 1); + sxe_add_namespace_name(return_value, ns[0]); ns++; } diff --git a/ext/simplexml/tests/025.phpt b/ext/simplexml/tests/025.phpt index 4c048c7ced..fc2516efa5 100755 --- a/ext/simplexml/tests/025.phpt +++ b/ext/simplexml/tests/025.phpt @@ -23,8 +23,19 @@ var_dump($sxe->getNamespaces(true)); var_dump($sxe->getDocNamespaces()); var_dump($sxe->getDocNamespaces(true)); +$xml =<< + +EOF; + +$sxe = simplexml_load_string($xml); + +var_dump($sxe->getNamespaces()); +var_dump($sxe->getDocNamespaces()); + ?> ===DONE=== + --EXPECTF-- array(1) { ["xhtml"]=> @@ -54,6 +65,14 @@ array(4) { ["baz"]=> string(9) "foobarbaz" } +array(1) { + [""]=> + string(28) "http://www.w3.org/1999/xhtml" +} +array(1) { + [""]=> + string(28) "http://www.w3.org/1999/xhtml" +} ===DONE=== --UEXPECTF-- array(1) { @@ -84,4 +103,12 @@ array(4) { [u"baz"]=> string(9) "foobarbaz" } +array(1) { + [u""]=> + string(28) "http://www.w3.org/1999/xhtml" +} +array(1) { + [u""]=> + string(28) "http://www.w3.org/1999/xhtml" +} ===DONE===