1

I'm working on DomDocument to build my XML. I need to output this header:

<p:FatturaElettronica versione="FPR12" xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd">

To achieve this result I have written this code:

$dom  = new \DomDocument("1.0", "UTF-8");
        $init = $dom->createElementNS('http://www.example.com/XFoo', 'p:FatturaElettronica');
        $init->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
        $init->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation', 'http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd');
        $version = "FPR12";
        $init->appendChild($dom->createAttribute('versione'))->appendChild($dom->createTextNode($version));

But the output is this:

    <p:FatturaElettronica xmlns:p="http://www.example.com/XFoo"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd
/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione
/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd" 
versione="FPR12">

Where am I wrong?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
wwhitewalter
  • 47
  • 1
  • 10

1 Answers1

1

Where am I wrong?

It's your goal that's wrong. Namespace prefixes must be declared if used. The XML in your goal uses namespaces prefixes but never declares them. This would leave the XML not namespace-well-formed.

The output that you are generating is namespace-well-formed and is what you should produce.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Sounds good: this is a copy/paste from Italian governement site.... http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/IT01234567890_FPA01.xml.... let me try to change output – wwhitewalter Jul 03 '18 at 19:46