0

I have the following piece of code in a Laravel controller:

public function toXMLSpreadsheet()
{
    $doc = new DOMDocument();
    $doc->load('xmlspreadsheet.xml');
    $table = $doc->getElementsByTagName('Table')->item(0);

    $applications = Application::all();
    foreach($applications as $application)
    {
        $f = $doc->createDocumentFragment();
        $f->appendXML("
            <Row>
                <Cell><Data ss:Type=\"Number\">{$application->id}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->nume}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->prenume}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->cnp}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->adresa}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->localitate}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->judet}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->telefon}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->email}</Data></Cell>
                <Cell><Data ss:Type=\"String\">{$application->nationalitate}</Data></Cell>
            </Row>
        ");
        $table->appendChild($f);
    }

    $doc->save('Inscrisi.xml');

}

and the following XML file:

<?xml version="1.0" encoding="UTF-8"?>

<?mso-application progid="Excel.Sheet"?>

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:html="http://www.w3.org/TR/REC-html40">

  <Worksheet ss:Name="Table1">
    <Table>
    </Table>
  </Worksheet>
</Workbook>

When I run the function in the controller I get an error like this:

production.ERROR: exception 'ErrorException' with message 'DOMDocumentFragment::appendXML(): namespace error : Namespace prefix ss for Type on Data is not defined' in E:\www\practica\app\controllers\ApplicationsController.php:105

Any idea why? The namespace ss is defined in the XML file.

Thank you!

rocordial
  • 17
  • 6

1 Answers1

0

You need to define the namespace for the 'ss' prefix in the XML fragment. Add a xmlns:ss with the correct namespace to the top element (row).

ThW
  • 19,120
  • 3
  • 22
  • 44