remove attribute from a node in Nexxmldocument in DELPHI

Asked

Viewed 219 times

1

I created an XML Document when creating the 2nd. Node 'Header' comes with the attribute from the previous Node.

The Node is created as <Header xmlns=""> how can I remove xmlns="" from Node

I used the following code

XML := NewXMLDocument;//initializate the interface
XML.Options := [doNodeAutoIndent];//activate the auto indentation
XML.Encoding:= 'Windows-1252';        //'ISO-8859-1';


XML := NewXMLDocument;
XML.Version := '1.0';
XML.Options := [doNodeAutoIndent,doAttrNull];
XML.Encoding := 'Windows-1252';
RootNode := XML.AddChild('AuditFile','');

RootNode.Attributes['xmlns:xsi'] := 'http://www.w3.org/2001/XMLSchema-instance';
RootNode.Attributes['xsi:schemaLocation'] := 'urn:OECD:StandardAuditFile-Tax:PT_1.04_01 .\SAFTPT1.04_01.xsd';
RootNode.Attributes['xmlns'] :='urn:OECD:StandardAuditFile-Tax:PT_1.04_01';

Node0 := RootNode.AddChild('Header');//add the task node
Node0.Resync;
  addChildToNode(Node0, 'Version','1.04_01', 10, True);
  addChildToNode(Node0, 'CompanyID'                ,'11111111111111', 50, True);
  addChildToNode(Node0, 'TaxRegistrationNumber'    ,'222222222222222222222',  9, True);
  addChildToNode(Node0, 'TaxAccountingBasis'       ,'F', 1, True );
  addChildToNode(Node0, 'CompanyName'              ,'444444444444444444444',100, True );  //Nome Registado
  addChildToNode(Node0, 'BusinessName'             ,'5555555555555555555555555555', 60, False );

The result was:

<?xml version="1.0" encoding="Windows-1252"?>
<AuditFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:OECD:StandardAuditFile-Tax:PT_1.04_01 .\SAFTPT1.04_01.xsd" xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.04_01">
    `**`<Header xmlns="">`**
    `<Version>1.04_01</Version>
    <CompanyID>11111111111111</CompanyID>
    <TaxRegistrationNumber>222222222</TaxRegistrationNumber>
    <TaxAccountingBasis>F</TaxAccountingBasis>
    <CompanyName>444444444444444444444</CompanyName>
    <BusinessName>5555555555555555555555555555</BusinessName>
  </Header>
</AuditFile>

How to do to the <Header xmlns=""> move on to <Header>

  • Have you tried the StringReplace(Texto_XML,'<Header xmlns="">', '<Header>', [rfReplaceAll]); ?

1 answer

0


I found the solution. Create a variable ns

var ns : String;

ns := 'urn:OECD:Standardauditfile-Tax:PT_1.04_01';

This -> ns has to be declared in all "node0"

ns := 'urn:OECD:StandardAuditFile-Tax:PT_1.04_01';

XML.Options := [doNodeAutoIndent];
RootNode := XML.AddChild('AuditFile');
RootNode.Attributes['xmlns:xsi'] := 'http://www.w3.org/2001/XMLSchema-instance';
RootNode.Attributes['xsi:schemaLocation'] := 'urn:OECD:StandardAuditFile-Tax:PT_1.04_01 .\SAFTPT1.04_01.xsd';

RootNode.DeclareNamespace('',ns);

Node0 := RootNode.AddChild('Header', ns);//add the task node
  addChildToNode(Node0, 'Version','1.04_01', 10, True);
  addChildToNode(Node0, 'CompanyID'                ,'11111111111111', 50, True);
  addChildToNode(Node0, 'TaxRegistrationNumber'    ,'222222222222222222222',  9, True);
Node0 := RootNode.AddChild('Semestre', ns);//add the task node
  addChildToNode(Node0, 'TaxAccountingBasis'       ,'F', 1, True );
  addChildToNode(Node0, 'CompanyName'              ,'444444444444444444444',100, True );  //Nome Registado
  addChildToNode(Node0, 'BusinessName'             ,'5555555555555555555555555555', 60, False );

Then the <Header> appears clean.

Browser other questions tagged

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