|
5 | 5 | namespace SimpleSAML\XML; |
6 | 6 |
|
7 | 7 | use DOMDocument; |
| 8 | +use DOMElement; |
8 | 9 | use SimpleSAML\XML\Assert\Assert; |
9 | 10 | use SimpleSAML\XML\Exception\IOException; |
10 | 11 | use SimpleSAML\XML\Exception\RuntimeException; |
11 | 12 | use SimpleSAML\XML\Exception\UnparseableXMLException; |
| 13 | +use SimpleSAML\XPath\XPath; |
12 | 14 |
|
13 | 15 | use function file_get_contents; |
14 | 16 | use function func_num_args; |
15 | 17 | use function libxml_clear_errors; |
16 | 18 | use function libxml_set_external_entity_loader; |
17 | 19 | use function libxml_use_internal_errors; |
18 | 20 | use function sprintf; |
| 21 | +use function strpos; |
19 | 22 |
|
20 | 23 | /** |
21 | 24 | * @package simplesamlphp/xml-common |
@@ -115,4 +118,83 @@ public static function create(string $version = '1.0', string $encoding = 'UTF-8 |
115 | 118 | { |
116 | 119 | return new DOMDocument($version, $encoding); |
117 | 120 | } |
| 121 | + |
| 122 | + |
| 123 | + /** |
| 124 | + * @param \DOMDocument $doc |
| 125 | + * @return \DOMDocument |
| 126 | + */ |
| 127 | + public static function normalizeDocument(DOMDocument $doc): DOMDocument |
| 128 | + { |
| 129 | + // Get the root element |
| 130 | + $root = $doc->documentElement; |
| 131 | + |
| 132 | + // Collect all xmlns attributes from the document |
| 133 | + $xpath = XPath::getXPath($doc); |
| 134 | + $xmlnsAttributes = []; |
| 135 | + |
| 136 | + // Register all namespaces to ensure XPath can handle them |
| 137 | + foreach ($xpath->query('//namespace::*') as $node) { |
| 138 | + $name = $node->nodeName === 'xmlns' ? 'xmlns' : $node->nodeName; |
| 139 | + if ($name !== 'xmlns:xml') { |
| 140 | + $xmlnsAttributes[$name] = $node->nodeValue; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // If no xmlns attributes found, return early with debug info |
| 145 | + if (empty($xmlnsAttributes)) { |
| 146 | + return $root->ownerDocument; |
| 147 | + } |
| 148 | + |
| 149 | + // Remove xmlns attributes from all elements |
| 150 | + $nodes = $xpath->query('//*[namespace::*]'); |
| 151 | + foreach ($nodes as $node) { |
| 152 | + if ($node instanceof DOMElement) { |
| 153 | + $attributesToRemove = []; |
| 154 | + foreach ($node->attributes as $attr) { |
| 155 | + if (strpos($attr->nodeName, 'xmlns') === 0 || $attr->nodeName === 'xmlns') { |
| 156 | + $attributesToRemove[] = $attr->nodeName; |
| 157 | + } |
| 158 | + } |
| 159 | + foreach ($attributesToRemove as $attrName) { |
| 160 | + $node->removeAttribute($attrName); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Add all collected xmlns attributes to the root element |
| 166 | + foreach ($xmlnsAttributes as $name => $value) { |
| 167 | + $root->setAttribute($name, $value); |
| 168 | + } |
| 169 | + |
| 170 | + // Return the normalized XML |
| 171 | + return static::fromString($root->ownerDocument->saveXML()); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + /** |
| 176 | + * @param \DOMElement $elt |
| 177 | + * @param string $prefix |
| 178 | + * @return string|null |
| 179 | + */ |
| 180 | + public static function lookupNamespaceURI(DOMElement $elt, string $prefix): ?string |
| 181 | + { |
| 182 | + // Get the root element |
| 183 | + $root = $elt->ownerDocument->documentElement; |
| 184 | + |
| 185 | + // Collect all xmlns attributes from the document |
| 186 | + $xpath = XPath::getXPath($elt->ownerDocument); |
| 187 | + |
| 188 | + // Register all namespaces to ensure XPath can handle them |
| 189 | + $xmlnsAttributes = []; |
| 190 | + foreach ($xpath->query('//namespace::*') as $node) { |
| 191 | + $xmlnsAttributes[$node->localName] = $node->nodeValue; |
| 192 | + } |
| 193 | + |
| 194 | + if (array_key_exists($prefix, $xmlnsAttributes)) { |
| 195 | + return $xmlnsAttributes[$prefix]; |
| 196 | + } |
| 197 | + |
| 198 | + return null; |
| 199 | + } |
118 | 200 | } |
0 commit comments