[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?
Hi.
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"/>
</a:AnAELement>
</ns1:DDIInstance>
program project1;
{$mode objfpc}{$H+}
uses
Classes, DOM, XMLWrite;
const
cArchive = 'ddi:archive:3_0';
var
XMLDoc: TXMLDocument;
DDIInstance: TDOMElement;
AnAElement, AnotherAElement: TDOMElement;
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.
//Here a New element using the Namespace that will be created.
AnAElement:=XMLDoc.CreateElementNS(cArchive, 'a:AnAELement');
DDIInstance.AppendChild(AnAElement);
//Another element of the same namespace, child of the prior, no
Namespace creation
//needed
AnotherAElement:=XMLDoc.CreateElementNS(cArchive, 'a:AnotherAELement');
AnAElement.AppendChild(AnotherAElement);
//The same as above, but with an Attribute
AnotherAElement.SetAttributeNS(cArchive, 'a:MyAtt', 'A value');
Xmldoc.AppendChild(DDIInstance);
WriteXMLFile(XMLDOc, '/tmp/test.xml');
end.
More information about the fpc-pascal
mailing list