3

I am trying to create XML in Delphi Berlin 10.1 and I need to obtain a file like this:

<?xml version="1.0" encoding="UTF-8"?>
<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" 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">
  <FatturaElettronicaHeader>
    <DatiTrasmissione>
      <IdTrasmittente>
        <IdPaese>IT</IdPaese>
        <IdCodice>01234567890</IdCodice>
      </IdTrasmittente>
      <ProgressivoInvio>00001</ProgressivoInvio>
      <FormatoTrasmissione>FPA12</FormatoTrasmissione>
      <CodiceDestinatario>AAAAAA</CodiceDestinatario>
    </DatiTrasmissione>
...

and I wrote this procedure

    procedure Tfattura2_new_form.Button1Click(Sender: TObject);
    Var
      XML : IXMLDOCUMENT;
      RootNode, CurNode, header[...]: IXMLNODE;
    begin
      XML := NewXMLDocument;
      XML.Encoding := 'utf-8';
      XML.Options := [doNodeAutoIndent]; 
      RootNode := XML.AddChild('FatturaElettronica');
      RootNode.Attributes['versione']:='FPA12';
      RootNode.DeclareNamespace('ds','http://www.w3.org/2000/09/xmldsig#');
      RootNode.DeclareNamespace('p','http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2');
      RootNode.DeclareNamespace('xsi','http://www.w3.org/2001/XMLSchema-instance');
   [...]
    
      header := RootNode.AddChild('FatturaElettronicaHeader');
      DatiTrasmissione := header.AddChild('DatiTrasmissione');
    
      IdTrasmittente :=  DatiTrasmissione.AddChild('IdTrasmittente');
          [...]
    XMl.SaveToFile('C:\file.xml');
end;

now the problemi is that I need to have prefix p in root node (p:FatturaElettronica... ) but if I don't know how: if I set

RootNode := XML.AddChild('p:FatturaElettronica');

in xml file I have prefix p: in every tag

<?xml version="1.0" encoding="utf-8"?>
<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" 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">
  <p:FatturaElettronicaHeader>
    <p:DatiTrasmissione>
      <p:IdTrasmittente>
        <p:IdPaese>IT</p:IdPaese>
        <p:IdCodice>09876543211</p:IdCodice>
      </p:IdTrasmittente>
      <p:ProgressivoInvio>23</p:ProgressivoInvio>
      <p:FormatoTrasmissione>FPR12</p:FormatoTrasmissione>
      <p:CodiceDestinatario>0000000</p:CodiceDestinatario>
    </p:DatiTrasmissione>

How I can fix it? Thankyou.

1
  • Ho avuto lo stesso problema. Risolto. Commented Sep 21, 2018 at 17:04

1 Answer 1

13

In short, you can't do this with IXMLNode.AddChild() alone.

When you use AddChild() to add a new child element, and you do not explicitly specify a namespace, the new child inherits the namespace of its parent element, and if the parent has a namespace prefix then that prefix gets inherited as well. This is hard-coded behavior in AddChild(), you can't change it. This is why you see the p: prefix on all of the child nodes.

If you use the overloaded AddChild() that takes a namespace as input, you can omit the prefix on the child element, and the parent's prefix will not be inherited. However, the new child element will have its own xmlns declaration, even if the namespace is the same as the parent's namespace:

header := RootNode.AddChild('FatturaElettronicaHeader', 'http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2');

Result:

<FatturaElettronicaHeader xmlns="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2">

You can't change this behavior, either.

But, there is a workaround - create the child node separately without any parent element or namespace, and then insert it as-is into the parent element, eg:

//header := RootNode.AddChild('FatturaElettronicaHeader');
header := XML.CreateElement('FatturaElettronicaHeader', '');
RootNode.ChildNodes.Add(header);

Then you can use AddChild() for subsequent child elements, and they will inherit the non-existent namespace of the FatturaElettronicaHeader element, eg:

XML := NewXMLDocument;
XML.Encoding := 'utf-8';
XML.Options := [doNodeAutoIndent];

RootNode := XML.AddChild('p:FatturaElettronica', 'http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2');
RootNode.Attributes['versione']:='FPA12';
RootNode.DeclareNamespace('ds','http://www.w3.org/2000/09/xmldsig#');
RootNode.DeclareNamespace('p','http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2');
RootNode.DeclareNamespace('xsi','http://www.w3.org/2001/XMLSchema-instance');

//header := RootNode.AddChild('FatturaElettronicaHeader');
header := XML.CreateElement('FatturaElettronicaHeader', '');
RootNode.ChildNodes.Add(header);

DatiTrasmissione := header.AddChild('DatiTrasmissione');
IdTrasmittente := DatiTrasmissione.AddChild('IdTrasmittente');

IdTrasmittente.AddChild('IdPaese').Text := 'IT';
IdTrasmittente.AddChild('IdCodice').Text := '01234567890';

DatiTrasmissione.AddChild('ProgressivoInvio').Text := '00001';
DatiTrasmissione.AddChild('FormatoTrasmissione').Text := 'FPA12';
DatiTrasmissione.AddChild('CodiceDestinatario').Text := 'AAAAAA';

XML.SaveToFile('C:\file.xml');

Result:

<?xml version="1.0" encoding="utf-8"?>
<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <FatturaElettronicaHeader>
    <DatiTrasmissione>
      <IdTrasmittente>
        <IdPaese>IT</IdPaese>
        <IdCodice>01234567890</IdCodice>
      </IdTrasmittente>
      <ProgressivoInvio>00001</ProgressivoInvio>
      <FormatoTrasmissione>FPA12</FormatoTrasmissione>
      <CodiceDestinatario>AAAAAA</CodiceDestinatario>
    </DatiTrasmissione>
  </FatturaElettronicaHeader>
</p:FatturaElettronica>
2
  • very good answer . i had same problem. thanks a lot ! regards Stefano Commented Sep 21, 2018 at 17:02
  • Thanks! I´ve been having this problem too. Now it´s ok! Commented Jan 30, 2019 at 14:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.