|
16 | 16 | use function restore_error_handler; |
17 | 17 | use function set_error_handler; |
18 | 18 | use function sprintf; |
19 | | -use function strpos; |
20 | 19 |
|
21 | 20 | /** |
22 | 21 | * @package simplesamlphp/xml-common |
@@ -168,53 +167,47 @@ public static function create(string $encoding = 'UTF-8'): Dom\XMLDocument |
168 | 167 | * @return \Dom\XMLDocument The same document instance, potentially modified. If the document has no root element |
169 | 168 | * or no namespace declarations to normalize, it is returned unchanged. |
170 | 169 | */ |
171 | | - public static function normalizeDocument(Dom\XMLDocument $doc): Dom\XMLDocument |
| 170 | + public static function normalizeDocument(Dom\Document $doc): Dom\Document |
172 | 171 | { |
173 | | - // Get the root element |
174 | 172 | $root = $doc->documentElement; |
175 | 173 | if ($root === null) { |
176 | 174 | return $doc; |
177 | 175 | } |
178 | 176 |
|
179 | 177 | $xpath = XPath::getXPath($doc); |
| 178 | + |
180 | 179 | $xmlnsAttributes = []; |
181 | 180 |
|
182 | | - // Collect namespace declarations needed for prefixed elements in the document |
183 | | - foreach ($xpath->query('//*[namespace::*]') as $node) { |
184 | | - if ($node instanceof Dom\Element) { |
185 | | - $name = 'xmlns:' . $node->prefix; |
186 | | - // Both prefix and namespaceURI NULL equals the default xmlns:xml namespace |
187 | | - if ($node->prefix !== null && $node->namespaceURI !== null) { |
188 | | - $xmlnsAttributes[$name] = $node->namespaceURI; |
| 181 | + // More reliable collection: look for attributes starting with xmlns on every element |
| 182 | + foreach ($xpath->query('//*') as $element) { |
| 183 | + if ($element instanceof Dom\Element) { |
| 184 | + foreach ($element->attributes as $attr) { |
| 185 | + if (str_starts_with($attr->nodeName, 'xmlns')) { |
| 186 | + $name = $attr->nodeName; |
| 187 | + $value = $attr->nodeValue; |
| 188 | + $xmlnsAttributes[$name] = $value; |
| 189 | + } |
189 | 190 | } |
190 | 191 | } |
191 | 192 | } |
192 | 193 |
|
193 | | - // If no xmlns attributes found, return early with debug info |
194 | | - if (empty($xmlnsAttributes)) { |
195 | | - return $doc; |
196 | | - } |
197 | | - |
198 | | - // Remove xmlns attributes from all elements (proper XMLNS namespace removal) |
199 | | - foreach ($xpath->query('//*[namespace::*]') as $node) { |
200 | | - if (!$node instanceof Dom\Element) { |
201 | | - continue; |
202 | | - } |
203 | | - |
204 | | - foreach ($node->attributes as $attr) { |
205 | | - if ($attr->namespaceURI === C::NS_XMLNS) { |
206 | | - $node->removeAttributeNS(C::NS_XMLNS, $attr->localName); |
207 | | - continue; |
| 194 | + // Remove from all elements |
| 195 | + foreach ($xpath->query('//*') as $element) { |
| 196 | + if ($element instanceof Dom\Element) { |
| 197 | + $toRemove = []; |
| 198 | + foreach ($element->attributes as $attr) { |
| 199 | + if (str_starts_with($attr->nodeName, 'xmlns')) { |
| 200 | + $toRemove[] = $attr->nodeName; |
| 201 | + } |
208 | 202 | } |
209 | 203 |
|
210 | | - if (strpos($attr->nodeName, 'xmlns') === 0 || $attr->nodeName === 'xmlns') { |
211 | | - // Fallback for implementations that still expose xmlns attrs without namespaceURI |
212 | | - $node->removeAttribute($attr->nodeName); |
| 204 | + foreach ($toRemove as $name) { |
| 205 | + $element->removeAttribute($name); |
213 | 206 | } |
214 | 207 | } |
215 | 208 | } |
216 | 209 |
|
217 | | - // Add all collected xmlns attributes to the root element |
| 210 | + // Re-attach to root |
218 | 211 | foreach ($xmlnsAttributes as $name => $value) { |
219 | 212 | $root->setAttributeNS(C::NS_XMLNS, $name, $value); |
220 | 213 | } |
|
0 commit comments