ZF2-7: Zend_Config_Xml support for namespaces attributes

Description

This little patch will add support in Zend_Config_Xml for namespaces attributes. This patch is for file Zend/Config/Xml.php in ZF 1.6.0RC3 \ \

--- Xml.php     2008-07-21 18:31:27.000000000 +0200
+++ XmlNew.php  2008-08-31 23:28:35.000000000 +0200
@@ -143,26 +143,32 @@
         $config = array();
 
         // Search for parent node values
-        if (count($xmlObject->attributes()) > 0) {
-            foreach ($xmlObject->attributes() as $key => $value) {
-                if ($key === 'extends') {
-                    continue;
-                }
+        $aNamespaces = array_merge(array(''), array_keys($xmlObject->getNamespaces(true)));
+        foreach($aNamespaces as $sNamespace) {
+            $aAttributes = empty($sNamespace) ? $xmlObject->attributes() : $xmlObject->attributes($sNamespace, true);
+            if (count($aAttributes) > 0) {
+                foreach ($aAttributes as $key => $value) {
+                    if (!empty($sNamespace)) {
+                        $key = sprintf('%s:%s', $sNamespace, $key);
+                    }
+                    if ($key === 'extends') {
+                        continue;
+                    }
 
-                $value = (string) $value;
+                    $value = (string) $value;
 
-                if (array_key_exists($key, $config)) {
-                    if (!is_array($config[$key])) {
-                        $config[$key] = array($config[$key]);
-                    }
+                    if (array_key_exists($key, $config)) {
+                        if (!is_array($config[$key])) {
+                            $config[$key] = array($config[$key]);
+                        }
 
-                    $config[$key][] = $value;
-                } else {
-                    $config[$key] = $value;
+                        $config[$key][] = $value;
+                    } else {
+                        $config[$key] = $value;
+                    }
                 }
             }
         }
-
         // Search for children
         if (count($xmlObject->children()) > 0) {
             foreach ($xmlObject->children() as $key => $value) {
@@ -176,7 +182,19 @@
                         $value = $this->_toArray($value);
                     }
                 } else {
-                    $value = (string) $value;
+                    // Search also for namespaces attributes
+                    $bNamespacesAttributesExists = false;
+                    $aNamespaces = array_keys($xmlObject->getNamespaces(true));
+                    foreach($aNamespaces as $sNamespace) {
+                        if (count($value->attributes($sNamespace, true)) > 0) {
+                            $value = $this->_toArray($value);
+                            $bNamespacesAttributesExists = true;
+                            break;
+                        }
+                    }
+                    if (!$bNamespacesAttributesExists) {
+                        $value = (string) $value;
+                    }
                 }
 
                 if (array_key_exists($key, $config)) {

Comments

What advantages does this give Zend_Config? Could you provide a use-case and sample config files?

Thanks,

Rob...

www.google.de/">
    
array
  'production' => 
    array
      'dummy' => string '' (length=0)
array
  'production' => 
    array
      'google:name' => string 'test' (length=4)
      'dummy' => string '' (length=0)

Reassigned to component maintainer

Moved to ZF2 as this is a feature improvement.

Ben: Any thoughts on this for ZF2?

The way XML (and every reader I know so far) works, is that unknown namespaces are skipped/ignored, and not blindly parsed. One reason is that this could lead to unwanted results. Closing the issue.