I want to export the contents of a TDataset into an XML file, and also deliver the accompaniyng .XSD file with it, containing the schema. The .XSD file is needed to get the right datatypes when importing the XML into Excel.
Creating the XMLDcoument with the actual data does not pose much of a problem, however, creating the SchemaDoc is.
We have tested what Excel needs in the XSD file, and the following is a (handcrafted) working copy of a valid .XSD file, and a sample XML file:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://ift.tt/tphNwY">
<xs:element name="DATAPACKET">
<xs:complexType>
<xs:sequence>
<xs:element name="ROWDATA">
<xs:complexType>
<xs:sequence>
<xs:element name="ROW" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="SomeID" type="xs:integer"/>
<xs:element name="SomeDate" type="xs:date"/>
<xs:element name="SomeName" type="xs:string"/>
<xs:element name="SomeTextValue" type="xs:string"/>
<xs:element name="SomeNumValue" type="xs:float"/>
<xs:element name="SomeDecValue" type="xs:decimal"/>
<xs:element name="SomeGroup" type="xs:string"/>
<xs:element name="SomeBoolean" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Datasetname" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DATAPACKET Version="2.0"
xmlns:xsi="http://ift.tt/ra1lAU" [^]
xsi:noNamespaceSchemaLocation="AssetsQuery.xsd">
<ROWDATA Datasetname="FirstDataset">
<ROW>
<SomeID>1</SomeID>
<SomeDate>1911-01-01</SomeDate>
<SomeName>SomeAsset_1</SomeName>
<SomeTextValue>"500001.55"</SomeTextValue>
<SomeNumValue>500001.55</SomeNumValue>
<SomeDecValue>-5001.55</SomeDecValue>
<SomeGroup>"Group_0"</SomeGroup>
<SomeBoolean>false</SomeBoolean>
</ROW>
<ROW>
<SomeID>2</SomeID>
<SomeDate>1914-01-01</SomeDate>
<SomeName>SomeAsset_2</SomeName>
<SomeTextValue>"00001.55"</SomeTextValue>
<SomeNumValue>00001.55</SomeNumValue>
<SomeDecValue>11.767646556477671</SomeDecValue>
<SomeGroup>Group_0</SomeGroup>
<SomeBoolean>true</SomeBoolean>
</ROW>
</ROWDATA>
</DATAPACKET>
I'm trying to create this Schema in Delphi in a IXMLSchemaDoc, using the following code:
procedure TSGXMLDatasetConverter.CreateSchema;
var
lComplexDataPacket, lComplexROWDATA, lComplexROW: IXMLComplexTypeDef;
lElement: IXMLElementDef;
i: Integer;
begin
FSchemaRoot := NewXMLSchema;
lComplexDatapacket := FSchemaRoot.SchemaDef.ComplexTypes.Add('', cmSequence);
lComplexDatapacket.ElementDefs.Add('ROWDATA');
lComplexROWDATA := FSchemaRoot.SchemaDef.ComplexTypes.Add('', cmSequence);
lComplexROWDATA.ElementDefs.Add('ROW').MaxOccurs := 'unbounded';
lComplexROW := FSchemaRoot.SchemaDef.ComplexTypes.Add('', cmSequence);
for i := 0 to Fdataset.FieldCount-1 do
begin
lElement := FSchemaRoot.SchemaDef.ElementDefs.Add('FieldName1');
lElement.Attributes['Type'] := 'xs:int';
lCOmplexROW.ElementDefs.ChildNodes.Add(lElement);
end;
lComplexROWDATA.ElementDefs.ChildNodes.Add(lComplexROW);
lComplexDataPacket.ElementDefs.ChildNodes.Add(lComplexROWDATA);
FSchemaRoot.SchemaDef.ElementDefs.Add('DATAPACKET').ChildNodes.Add(lComplexDataPacket);
end;
What I am wondering now is if the attribute setting of 'Type' is done correctly. The "xs" is done (implicitly) in the NewXMLSchema call, where "XS" is the default for the XSDPrefix parameter, and it seems to me I should not have to specify this int he Attributes[] assignment.
SHould I maybe use on of the interfaces like IXMLSimpleTypeDef provided in the XML.XMLSchema unit in stead of the IXMLElementDef?
Searching on the web reveals a lot of code in reading XSD but not many info on creating them, and the helpfiles of Delphi XE7 don't have anything in the Schema side of XML :-(
Any help appreciated Thanks Bas
No comments:
Post a Comment