[fpc-pascal] DOM and namespaces with prefix

Torsten Bonde Christiansen tc at epidata.info
Tue Jun 19 15:11:13 CEST 2012


Hi all.

I am working on implementing a lite export routine from our program to 
the DDI XML format: 
http://www.ddialliance.org/Specification/DDI-Lifecycle/3.1/XMLSchema/

A convinient way of working with the DDI format is to prefix elements 
using common set of prefix'es, but having spent most
of the day trying to I can only make it work with a single prefix.

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

uses
   Classes, DOM, XMLWrite;

var
   XMLDoc: TXMLDocument;
   DDIInstance: TDOMElement;
   Attr: TDOMAttr;

begin
   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.

   // now I would like to expand with eg: xmlns:a="ddi:archive:3_0"
   Attr := XMLDoc.CreateAttributeNS('ddi:archive:3_0', 'huh');
   Attr.Prefix := 'a';
   DDIInstance.SetAttributeNode(Attr);   // but this gives: 
<ns1:DDIInstance xmlns:ns1="ddi:instance:3_0" xmlns:a="ddi:archive:3_0" 
a:huh=""/>

   Xmldoc.AppendChild(DDIInstance);

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

How can i get the "xmlns:a="ddi:archive:3_0" without the additional 
attribute AND maintain the namespace + prefix in later elements?

Kind regards,
Torsten Bonde Christiansen.



More information about the fpc-pascal mailing list