@@ -140,26 +140,22 @@ public function decode(string $data, string $format, array $context = [])
140
140
// todo: throw an exception if the root node name is not correctly configured (bc)
141
141
142
142
if ($ rootNode ->hasChildNodes ()) {
143
- $ xpath = new \DOMXPath ($ dom );
144
- $ data = [];
145
- foreach ($ xpath ->query ('namespace::* ' , $ dom ->documentElement ) as $ nsNode ) {
146
- $ data ['@ ' .$ nsNode ->nodeName ] = $ nsNode ->nodeValue ;
143
+ $ data = $ this ->parseXml ($ rootNode , $ context );
144
+ if (\is_array ($ data )) {
145
+ $ data = $ this ->addXmlNamespaces ($ data , $ rootNode , $ dom );
147
146
}
148
147
149
- unset($ data ['@xmlns:xml ' ]);
150
-
151
- if (empty ($ data )) {
152
- return $ this ->parseXml ($ rootNode , $ context );
153
- }
154
-
155
- return array_merge ($ data , (array ) $ this ->parseXml ($ rootNode , $ context ));
148
+ return $ data ;
156
149
}
157
150
158
151
if (!$ rootNode ->hasAttributes ()) {
159
152
return $ rootNode ->nodeValue ;
160
153
}
161
154
162
- return array_merge ($ this ->parseXmlAttributes ($ rootNode , $ context ), ['# ' => $ rootNode ->nodeValue ]);
155
+ $ data = array_merge ($ this ->parseXmlAttributes ($ rootNode , $ context ), ['# ' => $ rootNode ->nodeValue ]);
156
+ $ data = $ this ->addXmlNamespaces ($ data , $ rootNode , $ dom );
157
+
158
+ return $ data ;
163
159
}
164
160
165
161
/**
@@ -344,6 +340,19 @@ private function parseXmlValue(\DOMNode $node, array $context = [])
344
340
return $ value ;
345
341
}
346
342
343
+ private function addXmlNamespaces (array $ data , \DOMNode $ node , \DOMDocument $ document ): array
344
+ {
345
+ $ xpath = new \DOMXPath ($ document );
346
+
347
+ foreach ($ xpath ->query ('namespace::* ' , $ node ) as $ nsNode ) {
348
+ $ data ['@ ' .$ nsNode ->nodeName ] = $ nsNode ->nodeValue ;
349
+ }
350
+
351
+ unset($ data ['@xmlns:xml ' ]);
352
+
353
+ return $ data ;
354
+ }
355
+
347
356
/**
348
357
* Parse the data and convert it to DOMElements.
349
358
*
0 commit comments