Tag ns1: repeats on all other tags (XML)

Asked

Viewed 588 times

0

I’m trying to make an XML but the ns1 tag repeats in all xml ready, has to solve this in an easier way?

Example of the code:

function TRPS.geraXML:TStrings;
var XMLDoc    : TXMLDocument;
ANode     : IXMLNode;
begin
try
   XMLDoc     := TXMLDocument.Create(Application);
   With XMLDoc do
   begin
     Active   := True;
     Version  := '1.0';
     Encoding := 'UTF-8';

     AddChild('ns1:ReqEnvioLoteRPS');
     DocumentElement.Attributes['xmlns:ns1']          := 'http://localhost:8080/WsNFe2/lote';
     DocumentElement.Attributes['xmlns:tipos']        := 'http://localhost:8080/WsNFe2/tp';
     DocumentElement.Attributes['xmlns:xsi']          := 'http://www.w3.org/2001/XMLSchema-instance';
     DocumentElement.Attributes['xsi:schemaLocation'] := 'http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ReqEnvioLoteRPS.xsd';

    ANode := DocumentElement;
    with ANode.addChild('Cabecalho') do
    begin
       AddChild('CodCidade').NodeValue            := Self.FPrestadorCidadeCodigoIBGE;
       AddChild('CPFCNPJRemetente').NodeValue     := Self.FPrestadorCPFCNPJ;
       AddChild('RazaoSocialRemetente').NodeValue := Self.FPrestadorRazaoSocial;
       AddChild('Transacao').NodeValue            := Self.FRPSTransacao;
       AddChild('DTInicio').NodeValue             := FormatDateTime('yyyy-mm-dd',Self.FLoteDtInicio);
       AddChild('DTFim').NodeValue                := FormatDateTime('yyyy-mm-dd',Self.FLoteDTFim);
       AddChild('QtdRPS').NodeValue               := Self.FLoteQtdeRPS;
       AddChild('ValorTotalServicos').NodeValue   := VirgulaPorPonto(FormatFloat('#0.00',Self.FLoteValorTotalServicos));
       AddChild('ValorTotalDeducoes').NodeValue   := VirgulaPorPonto(FormatFloat('#0.00',Self.FLoteValorTotalDeducoes));
       AddChild('Versao').NodeValue               := Self.LoteVersao;
       AddChild('MetodoEnvio').NodeValue          := Self.FLoteMetodoEnvio;
   end;
   Result := XMLDoc.XML;
finally
  FreeAndNil(XMLDoc);
end;
end;

example of XML delivered has the tag n1 everywhere

   <?xml version="1.0"?>

   -<ns1:ReqEnvioLoteRPS xsi:schemaLocation="http://localhost:8080/WsNFe2/lote http://localhost:8080/WsNFe2/xsd/ReqEnvioLoteRPS.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tipos="http://localhost:8080/WsNFe2/tp" xmlns:ns1="http://localhost:8080/WsNFe2/lote">

   -<ns1:Cabecalho>
       <ns1:CodCidade>0</ns1:CodCidade>
       <ns1:CPFCNPJRemetente/>
       <ns1:RazaoSocialRemetente/>
       <ns1:Transacao/>
       <ns1:DTInicio>1899-12-30</ns1:DTInicio>
       <ns1:DTFim>1899-12-30</ns1:DTFim>
       <ns1:QtdRPS>0</ns1:QtdRPS>
       <ns1:ValorTotalServicos>0.00</ns1:ValorTotalServicos>
       <ns1:ValorTotalDeducoes>0.00</ns1:ValorTotalDeducoes>
       <ns1:Versao>0</ns1:Versao>
       <ns1:MetodoEnvio/>
       </ns1:Cabecalho>
   </ns1:ReqEnvioLoteRPS>

1 answer

1

Remove the :ns1 from here: Documentelement.Attributes['xmlns:ns1'] := And from here: AddChild('ns1:ReqEnvioLoteRPS'). Since you are basing your xml on a schema, it may be that your framework still manages the xml with this namespace indicator. It’s not wrong what you want to do, but it would be interesting to keep ns1, since it indicates which xml schema the Complex type belongs to. This is good practice, since it can happen that in the same xml there are two or more Complex types, which can even conflict. And without origin indication, the interpretation of your xml is impaired. Take a look at this link for best emais full explanation: http://www.w3schools.com/xml/xml_namespaces.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.