Issues

ZF2-7: Zend_Config_Xml support for namespaces attributes

Issue Type: Improvement Created: 2008-08-31T14:30:55.000+0000 Last Updated: 2012-04-29T10:49:07.000+0000 Status: Resolved Fix version(s): Reporter: Aldo Armiento (aldo) Assignee: Ben Scholzen (dasprid) Tags: - Zend\Config

Related issues: Attachments: - patch.diff

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 \ \

<pre class="literal">
--- 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

Posted by Rob Allen (rob) on 2009-03-21T13:08:05.000+0000

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

Thanks,

Rob...

Posted by Jan Pieper (jpieper) on 2009-11-20T01:24:24.000+0000

<pre class="highlight">www.google.de/">



<pre class="highlight">array
  'production' => 
    array
      'dummy' => string '' (length=0)


<pre class="highlight">array
  'production' => 
    array
      'google:name' => string 'test' (length=4)
      'dummy' => string '' (length=0)

Posted by Thomas Weidner (thomas) on 2010-03-21T10:25:15.000+0000

Reassigned to component maintainer

Posted by Rob Allen (rob) on 2011-05-27T01:58:28.000+0000

Moved to ZF2 as this is a feature improvement.

Posted by Rob Allen (rob) on 2012-04-28T16:13:32.000+0000

Ben: Any thoughts on this for ZF2?

Posted by Ben Scholzen (dasprid) on 2012-04-29T10:49:07.000+0000

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.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts