[fpc-pascal] DOM and namespaces with prefix

Daniel Gaspary dgaspary at gmail.com
Thu Jun 21 20:51:29 CEST 2012

On Tue, Jun 19, 2012 at 10:11 AM, Torsten Bonde Christiansen
<tc at epidata.info> wrote:
> How can i get the "xmlns:a="ddi:archive:3_0" without the additional
> attribute AND maintain the namespace + prefix in later elements?


It's hard to test without a (small please) comlete XML example because
the NS attribute is added when it's needed. So, you need to add an
attribute or a child element that uses the Namespace.

The XML below is created with the modfied program ahead. But I believe
a method to create a namespace attribute would be useful.

<?xml version="1.0" encoding="utf-8"?>
<ns1:DDIInstance xmlns:ns1="ddi:instance:3_0">
  <a:AnAELement xmlns:a="ddi:archive:3_0">
    <a:AnotherAELement a:MyAtt="A value"/>

program project1;
{$mode objfpc}{$H+}

 Classes, DOM, XMLWrite;

     cArchive = 'ddi:archive:3_0';
 XMLDoc: TXMLDocument;
 DDIInstance: TDOMElement;
 AnAElement, AnotherAElement: TDOMElement;

 XMLDoc := TXMLDocument.Create;
 DDIInstance := XMLDoc.CreateElementNS('ddi:instance:3_0', 'DDIInstance');
 DDIInstance.Prefix := 'ns1';        // this gives me:
<ns1:DDIInstance xmlns:ns1="ddi:instance:3_0"> which is great.

 //Here a New element using the Namespace that will be created.
 AnAElement:=XMLDoc.CreateElementNS(cArchive, 'a:AnAELement');

 //Another element of the same namespace, child of the prior, no
Namespace creation
 AnotherAElement:=XMLDoc.CreateElementNS(cArchive, 'a:AnotherAELement');

 //The same as above, but with an Attribute
 AnotherAElement.SetAttributeNS(cArchive, 'a:MyAtt', 'A value');


 WriteXMLFile(XMLDOc, '/tmp/test.xml');

More information about the fpc-pascal mailing list