Hello Rob --

Are you sure this is right?  The xmlns prefix is supposed to be bound
to http://www.w3.org/2000/xmlns/, so I would think you would need to
use getAttributeNS() here.

Or are you allowed to use getAttribute() to pull out namespaced
attributes with prefixes?

Either way, what's the getAttributeNS() version of this supposed to
be? And what would you do to get the default namespace URI?

getAttributeNS('http://www.w3.org/2000/xmlns/','');

This one works now, but I don't know if that's spec.

-adam

On Wed, 27 Sep 2006, Rob Richards wrote:

> rrichards             Wed Sep 27 10:31:24 2006 UTC
>
>   Added files:                 (Branch: PHP_5_2)
>     /php-src/ext/dom/tests    bug38949.phpt nsdoc.xml
>
>   Modified files:
>     /php-src/ext/dom  element.c
>   Log:
>   fix bug #38949 (Cannot get xmlns value attribute)
>   add test
>
> http://cvs.php.net/viewvc.cgi/php-src/ext/dom/element.c?r1=1.36.2.4.2.4&r2=1.36.2.4.2.5&diff_format=u
> Index: php-src/ext/dom/element.c
> diff -u php-src/ext/dom/element.c:1.36.2.4.2.4 
> php-src/ext/dom/element.c:1.36.2.4.2.5
> --- php-src/ext/dom/element.c:1.36.2.4.2.4    Fri Sep 15 07:44:20 2006
> +++ php-src/ext/dom/element.c Wed Sep 27 10:31:24 2006
> @@ -17,7 +17,7 @@
>     +----------------------------------------------------------------------+
>  */
>
> -/* $Id: element.c,v 1.36.2.4.2.4 2006/09/15 07:44:20 tony2001 Exp $ */
> +/* $Id: element.c,v 1.36.2.4.2.5 2006/09/27 10:31:24 rrichards Exp $ */
>
>  #ifdef HAVE_CONFIG_H
>  #include "config.h"
> @@ -188,7 +188,7 @@
>
>  /* }}} */
>
> -static xmlAttrPtr dom_get_dom1_attribute(xmlNodePtr elem, xmlChar *name) {
> +static xmlNodePtr dom_get_dom1_attribute(xmlNodePtr elem, xmlChar *name) {
>      int len;
>      const xmlChar *nqname;
>
> @@ -196,15 +196,37 @@
>       if (nqname != NULL) {
>               xmlNsPtr ns;
>               xmlChar *prefix = xmlStrndup(name, len);
> +             if (prefix && xmlStrEqual(prefix, "xmlns")) {
> +                     ns = elem->nsDef;
> +                     while (ns) {
> +                             if (xmlStrEqual(ns->prefix, nqname)) {
> +                                     break;
> +                             }
> +                             ns = ns->next;
> +                     }
> +                     xmlFree(prefix);
> +                     return (xmlNodePtr)ns;
> +             }
>               ns = xmlSearchNs(elem->doc, elem, prefix);
>               if (prefix != NULL) {
>                       xmlFree(prefix);
>               }
>               if (ns != NULL) {
> -                     return xmlHasNsProp(elem, nqname, ns->href);
> +                     return (xmlNodePtr)xmlHasNsProp(elem, nqname, ns->href);
> +             }
> +     } else {
> +             if (xmlStrEqual(name, "xmlns")) {
> +                     xmlNsPtr nsPtr = elem->nsDef;
> +                     while (nsPtr) {
> +                             if (nsPtr->prefix == NULL) {
> +                                     return (xmlNodePtr)nsPtr;
> +                             }
> +                             nsPtr = nsPtr->next;
> +                     }
> +                     return NULL;
>               }
>       }
> -     return xmlHasNsProp(elem, name, NULL);
> +     return (xmlNodePtr)xmlHasNsProp(elem, name, NULL);
>  }
>
>  /* {{{ proto string dom_element_get_attribute(string name);
> @@ -217,7 +239,7 @@
>       xmlNode *nodep;
>       char *name, *value = NULL;
>       dom_object *intern;
> -     xmlAttrPtr attr;
> +     xmlNodePtr attr;
>       int name_len;
>
>       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), 
> "Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
> @@ -228,9 +250,14 @@
>
>       attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
>       if (attr) {
> -             if (attr->type == XML_ATTRIBUTE_NODE) {
> +             switch (attr->type) {
> +                     case XML_ATTRIBUTE_NODE:
>                       value = xmlNodeListGetString(attr->doc, attr->children, 
> 1);
> -             } else {
> +                             break;
> +                     case XML_NAMESPACE_DECL:
> +                             value = xmlStrdup(((xmlNsPtr)attr)->href);
> +                             break;
> +                     default:
>                       value = 
> xmlStrdup(((xmlAttributePtr)attr)->defaultValue);
>               }
>       }
> @@ -253,7 +280,7 @@
>  {
>       zval *id, *rv = NULL;
>       xmlNode *nodep;
> -     xmlAttr *attr;
> +     xmlNodePtr attr = NULL;
>       int ret, name_len, value_len;
>       dom_object *intern;
>       char *name, *value;
> @@ -275,16 +302,30 @@
>       }
>
>       attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
> -     if (attr != NULL && attr->type != XML_ATTRIBUTE_DECL) {
> +     if (attr != NULL) {
> +             switch (attr->type) {
> +                     case XML_ATTRIBUTE_NODE:
>               node_list_unlink(attr->children TSRMLS_CC);
> +                             break;
> +                     case XML_NAMESPACE_DECL:
> +                             RETURN_FALSE;
> +             }
> +
> +     }
> +
> +     if (xmlStrEqual((xmlChar *)name, "xmlns")) {
> +             if (xmlNewNs(nodep, (xmlChar *)value, NULL)) {
> +                     RETURN_TRUE;
> +             }
> +     } else {
> +             attr = (xmlNodePtr)xmlSetProp(nodep, name, value);
>       }
> -     attr = xmlSetProp(nodep, name, value);
>       if (!attr) {
>               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such attribute 
> '%s'", name);
>               RETURN_FALSE;
>       }
>
> -     DOM_RET_OBJ(rv, (xmlNodePtr) attr, &ret, intern);
> +     DOM_RET_OBJ(rv, attr, &ret, intern);
>
>  }
>  /* }}} end dom_element_set_attribute */
> @@ -297,8 +338,7 @@
>  PHP_FUNCTION(dom_element_remove_attribute)
>  {
>       zval *id;
> -     xmlNode *nodep;
> -     xmlAttr *attrp;
> +     xmlNodePtr nodep, attrp;
>       dom_object *intern;
>       int name_len;
>       char *name;
> @@ -319,14 +359,18 @@
>               RETURN_FALSE;
>       }
>
> -     if (attrp->type != XML_ATTRIBUTE_DECL) {
> -             if (php_dom_object_get_data((xmlNodePtr) attrp) == NULL) {
> +     switch (attrp->type) {
> +             case XML_ATTRIBUTE_NODE:
> +                     if (php_dom_object_get_data(attrp) == NULL) {
>                       node_list_unlink(attrp->children TSRMLS_CC);
> -                     xmlUnlinkNode((xmlNodePtr) attrp);
> -                     xmlFreeProp(attrp);
> +                             xmlUnlinkNode(attrp);
> +                             xmlFreeProp((xmlAttrPtr)attrp);
>               } else {
> -                     xmlUnlinkNode((xmlNodePtr) attrp);
> +                             xmlUnlinkNode(attrp);
>               }
> +                     break;
> +             case XML_NAMESPACE_DECL:
> +                     RETURN_FALSE;
>       }
>
>       RETURN_TRUE;
> @@ -341,8 +385,7 @@
>  PHP_FUNCTION(dom_element_get_attribute_node)
>  {
>       zval *id, *rv = NULL;
> -     xmlNode *nodep;
> -     xmlAttr  *attrp;
> +     xmlNodePtr nodep, attrp;
>       int name_len, ret;
>       dom_object *intern;
>       char *name;
> @@ -358,6 +401,25 @@
>               RETURN_FALSE;
>       }
>
> +     if (attrp->type == XML_NAMESPACE_DECL) {
> +             xmlNsPtr curns;
> +             xmlNodePtr nsparent;
> +
> +             nsparent = attrp->_private;
> +             curns = xmlNewNs(NULL, attrp->name, NULL);
> +             if (attrp->children) {
> +                     curns->prefix = xmlStrdup((xmlChar *) attrp->children);
> +             }
> +             if (attrp->children) {
> +                     attrp = xmlNewDocNode(nodep->doc, NULL, (xmlChar *) 
> attrp->children, attrp->name);
> +             } else {
> +                     attrp = xmlNewDocNode(nodep->doc, NULL, "xmlns", 
> attrp->name);
> +             }
> +             attrp->type = XML_NAMESPACE_DECL;
> +             attrp->parent = nsparent;
> +             attrp->ns = curns;
> +     }
> +
>       DOM_RET_OBJ(rv, (xmlNodePtr) attrp, &ret, intern);
>  }
>  /* }}} end dom_element_get_attribute_node */
> @@ -879,7 +941,7 @@
>       dom_object *intern;
>       char *name;
>       int name_len;
> -     xmlAttrPtr attr;
> +     xmlNodePtr attr;
>
>       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), 
> "Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
>               return;
>
> http://cvs.php.net/viewvc.cgi/php-src/ext/dom/tests/bug38949.phpt?view=markup&rev=1.1
> Index: php-src/ext/dom/tests/bug38949.phpt
> +++ php-src/ext/dom/tests/bug38949.phpt
>
> http://cvs.php.net/viewvc.cgi/php-src/ext/dom/tests/nsdoc.xml?view=markup&rev=1.1
> Index: php-src/ext/dom/tests/nsdoc.xml
> +++ php-src/ext/dom/tests/nsdoc.xml
>
>

-- 
[EMAIL PROTECTED] | http://www.trachtenberg.com
author of o'reilly's "upgrading to php 5" and "php cookbook"
avoid the holiday rush, buy your copies today!

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

Reply via email to