diff --git a/src/PhpSpreadsheet/Reader/Xml.php b/src/PhpSpreadsheet/Reader/Xml.php index 11aa1df3bd..452ec13564 100644 --- a/src/PhpSpreadsheet/Reader/Xml.php +++ b/src/PhpSpreadsheet/Reader/Xml.php @@ -324,6 +324,8 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet) $xml = $this->trySimpleXMLLoadString($pFilename); $namespaces = $xml->getNamespaces(true); + $namespacesDoc = $xml->getDocNamespaces(true); + $namespaces = array_merge($namespaces, $namespacesDoc); $docProps = $spreadsheet->getProperties(); if (isset($xml->DocumentProperties[0])) { diff --git a/tests/PhpSpreadsheetTests/Reader/Xml/XmlTest.php b/tests/PhpSpreadsheetTests/Reader/Xml/XmlTest.php index 2b66c7b430..b28855548f 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xml/XmlTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xml/XmlTest.php @@ -47,4 +47,18 @@ public function testHyperlinksAltCharset(): void self::assertEquals('PhpSpreadsheet', $hyperlink->getValue()); self::assertEquals('https://phpspreadsheet.readthedocs.io', $hyperlink->getHyperlink()->getUrl()); } + + /** + * Check if it can read XML if some namespaces are defined in root node but never used. + */ + public function testNoNamespaceError(): void + { + $reader = new Xml(); + $spreadsheet = $reader->load('tests/data/Reader/Xml/ReadNamespacesNeverUsed.xml'); + $spreadsheet->setActiveSheetIndex(0); + self::assertEquals('First Column', $spreadsheet->getActiveSheet()->getCell('A1')->getValue()); + self::assertNull($spreadsheet->getActiveSheet()->getCell('A2')->getValue()); + self::assertEquals('Second Column', $spreadsheet->getActiveSheet()->getCell('B1')->getValue()); + self::assertEquals('Third Column', $spreadsheet->getActiveSheet()->getCell('C1')->getValue()); + } } diff --git a/tests/data/Reader/Xml/ReadNamespacesNeverUsed.xml b/tests/data/Reader/Xml/ReadNamespacesNeverUsed.xml new file mode 100644 index 0000000000..802a018ba5 --- /dev/null +++ b/tests/data/Reader/Xml/ReadNamespacesNeverUsed.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + First Column + + + Second Column + + + Third Column + + +
+
+
\ No newline at end of file