Skip to content

Undefined namespace in Xlsx reader for VML/Shapes #4505

@benmag

Description

@benmag

I've been having an issue where trying to read an XLSX file was throwing a SimpleXMLElement::xpath(): Undefined namespace prefix

[2025-06-09 00:56:18] local.ERROR: SimpleXMLElement::xpath(): Undefined namespace prefix {"userId":"1","exception":"[object] (ErrorException(code: 0): SimpleXMLElement::xpath(): Undefined namespace prefix at /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php:1206)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(255): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}()
#2 /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php(1206): SimpleXMLElement->xpath()
#3 /var/www/html/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/BaseReader.php(199): PhpOffice\\PhpSpreadsheet\\Reader\\Xlsx->loadSpreadsheetFromFile()

What are the steps to reproduce?
php 8.3

phpspreadsheet v4.3.1

Attempt to read a XLSX file that triggers the Shapes path (https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Reader/Xlsx.php#L1177)

For reference the XML of my $shapes[0] that's resulting in the error is:

<ns1:shape type="#_x0000_t202" style="position:absolute; margin-left:59.25pt;margin-top:1.5pt;width:144px;height:79px;z-index:1;visibility:hidden" fillcolor="#ffffe1" ns0:insetmode="auto" id="_x0000_s1026">
	<ns1:fill color2="#ffffe1"/>
	<ns1:shadow color="black" obscured="t"/>
	<ns1:path ns0:connecttype="none"/>
	<ns1:textbox style="mso-direction-alt:auto">
		<div style="text-align:left"/>
	</ns1:textbox>
	<ns2:ClientData ObjectType="Note">
		<ns2:MoveWithCells/>
		<ns2:SizeWithCells/>
		<ns2:AutoFill>False</ns2:AutoFill>
		<ns2:Row>18</ns2:Row>
		<ns2:Column>14</ns2:Column>
	</ns2:ClientData>
</ns1:shape>  

Solution

Registering the following namespaces under the existing $shape->registerXPathNamespace('v', Namespaces::URN_VML) (https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Reader/Xlsx.php#L1180) allow it to gracefully handle this scenario

$shape->registerXPathNamespace('x', Namespaces::URN_VML);
$shape->registerXPathNamespace('o', Namespaces::URN_MSOFFICE);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions