Can I use xsd:complexContent with the Delphi XML Binding Wizard?
Yes, xsd:complexContent
can be used.
I know Delphi has its flaws, but I don't blame Delphi for this schema. XSD is a rich schema language, and so is Delphi's OO class. Parts of two worlds overlap, but there are parts that won't. XML databinding is an act of translating an XML schema into OO class structure, so the schema has to be concrete enough to be expressed as class.
In this example, you are saying that TestType
matches any type so long as it has a string
attribute called Name
. An XML validator may be ok with that kind of definition, but it's hard to define that in a single-inheritance model, since foo:Animal
, foo:Plant
, and foo:Mineral
may all have Name
attribute.
I defined an empty complexType called TestBaseType
and that generated class perfectly fine.
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema targetNamespace="http://example.org/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:complexType name="TestBaseType">
<xsd:sequence>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="TestType">
<xsd:complexContent>
<xsd:restriction base="TestBaseType">
<xsd:attribute name="Name" type="xsd:string"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
This generated the following code:
unit test;
interface
uses xmldom, XMLDoc, XMLIntf;
type
{ Forward Decls }
IXMLTestBaseType = interface;
IXMLTestType = interface;
{ IXMLTestBaseType }
IXMLTestBaseType = interface(IXMLNode)
['{0FBC1D84-DA5E-4315-83A9-B5FFE9528969}']
end;
{ IXMLTestType }
IXMLTestType = interface(IXMLTestBaseType)
['{12E35067-516F-4457-8C62-4131CA60D706}']
{ Property Accessors }
function Get_Name: WideString;
procedure Set_Name(Value: WideString);
{ Methods & Properties }
property Name: WideString read Get_Name write Set_Name;
end;
{ Forward Decls }
TXMLTestBaseType = class;
TXMLTestType = class;
{ TXMLTestBaseType }
TXMLTestBaseType = class(TXMLNode, IXMLTestBaseType)
protected
{ IXMLTestBaseType }
end;
{ TXMLTestType }
TXMLTestType = class(TXMLTestBaseType, IXMLTestType)
protected
{ IXMLTestType }
function Get_Name: WideString;
procedure Set_Name(Value: WideString);
end;
implementation
{ TXMLTestBaseType }
{ TXMLTestType }
function TXMLTestType.Get_Name: WideString;
begin
Result := AttributeNodes['Name'].Text;
end;
procedure TXMLTestType.Set_Name(Value: WideString);
begin
SetAttribute('Name', Value);
end;
end.