简体   繁体   English

Delphi Berlin 10.1 IXMLDOCUMENT根节点前缀

[英]Delphi Berlin 10.1 IXMLDOCUMENT root node prefix

I am trying to create XML in Delphi Berlin 10.1 and I need to obtain a file like this: 我想在Delphi Berlin 10.1中创建XML,我需要获取这样的文件:

<?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 现在问题是我需要在根节点中有前缀p(p:FatturaElettronica ...)但如果我不知道如何:如果我设置

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

in xml file I have prefix p: in every tag 在xml文件中,我在每个标签中都有前缀p :.

<?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>03444630135</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. 谢谢。

In short, you can't do this with IXMLNode.AddChild() alone. 简而言之,单独使用IXMLNode.AddChild()就无法做到这一点。

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. 当您使用AddChild()添加新的子元素,并且您没有显式指定命名空间时,新子元素继承其父元素的命名空间,如果父元素具有命名空间前缀,则该前缀也会被继承。 This is hard-coded behavior in AddChild() , you can't change it. 这是AddChild()硬编码行为,您无法更改它。 This is why you see the p: prefix on all of the child nodes. 这就是您在所有子节点上看到p:前缀的原因。

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. 如果使用带有命名空间作为输入的重载AddChild() ,则可以省略子元素上的前缀,并且不会继承父节点的前缀。 However, the new child element will have its own xmlns declaration, even if the namespace is the same as the parent's namespace: 但是,新的子元素将具有自己的xmlns声明,即使命名空间与父命名空间相同:

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: 然后,您可以将AddChild()用于后续子元素,并且它们将继承FatturaElettronicaHeader元素的不存在的命名空间,例如:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM