SimpleXML root node prefix php

611 Views Asked by At

I'm trying to create XML with php: this is true XML

<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12" >
 <FatturaElettronicaHeader>
  <DatiTrasmissione>
   <IdTrasmittente>
    <IdPaese>IT</IdPaese>
    <IdCodice>01234567890</IdCodice>
   </IdTrasmittente>
   <ProgressivoInvio>00001</ProgressivoInvio>
   <FormatoTrasmissione>FPA12</FormatoTrasmissione>
   <CodiceDestinatario>AAAAAA</CodiceDestinatario>
  </DatiTrasmissione>

My code:

$xml = new SimpleXMLElement('<p:FatturazioneElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" />');
$xml->addAttribute("versione","FPA12");
$xml->addAttribute("xmlns:xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
$FatturaElettronicaHeader = $xml->addChild('FatturaElettronicaHeader');
$DatiTrasmissione=$FatturaElettronicaHeader->addChild('DatiTrasmissione');
$IdTrasmittente=$DatiTrasmissione->addChild('IdTrasmittente');
$IdTrasmittente->addChild('IdPaese', 'IT');
$IdTrasmittente->addChild('IdCodice','01234567890');

$ProgressivoInvio=$DatiTrasmissione->addChild('ProgressivoInvio', '00001');
$FormatoTrasmissione=$DatiTrasmissione->addChild('DatiTrasmissione', 'FPA12');
$CodiceDestinatario=$DatiTrasmissione->addChild('CodiceDestinatario', 'AAAAAA');

in my xml file i have prefix p: in every tag.

i need to have prefix p in root node (p:FatturaElettronica).

I don't know how to do it.

<p:FatturazioneElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12">
 <p:FatturaElettronicaHeader>
   <p:DatiTrasmissione>
    <p:IdTrasmittente>
     <p:IdPaese>IT</p:IdPaese>
     <p:IdCodice>01234567890</p:IdCodice>
    </p:IdTrasmittente>
    <p:ProgressivoInvio>00001</p:ProgressivoInvio>
   <p:DatiTrasmissione>FPA12</p:DatiTrasmissione>
   <p:CodiceDestinatario>AAAAAA</p:CodiceDestinatario>
 </p:DatiTrasmissione>
1

There are 1 best solutions below

6
Nigel Ren On BEST ANSWER

The problem with SimpleXML is that if you don't specify the namespace of an element when adding it, it assumes the namespace of the parent node (hence the p:). To add it to the default namespace (i.e. without a prefix) there are a couple of things you will need to change.

First is to add a default namespace declaration at the root element...

$xml = new SimpleXMLElement('<p:FatturazioneElettronica 
      xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
      xmlns:p="http://microsoft.com/wsdl/types/" 
      xmlns="http://dummy.com" />');

I've just added is as xmlns="http://dummy.com" near the end.

Then when adding the first element to the document, add this to the newly defined default namespace...

$FatturaElettronicaHeader = $xml->addChild('FatturaElettronicaHeader', 
            null, 'http://dummy.com');