I am trying to create an XML schema in which a lot of types are sharing some 'magic numbers'.
Instead of having to change my schema in several locations if/when these magic numbers change, I would like to pull them out into some kind of constant definition.
I have played around with adding a DTD to my schema and declaring some entities here. But I am by no means an expert on DTD, and while it seems to work in a C# application that uses the schema, there is also a native Win32 application that uses the same schema with msxml 4.0 where this blows up...
Does anyone have experience with extending the schema definition this way (can it be done), or is there a better way?
(EDIT: An example)
Example XML:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE schema [
<!ENTITY SomeMagicNumber "10">]>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="MySimpleType">
<xs:restriction base="xs:int">
<xs:maxInclusive value="&SomeMagicNumber;" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="MyIntegers">
<xs:sequence>
<xs:element name="value" type="xs:int" maxOccurs="&SomeMagicNumber;" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="MyFloats">
<xs:sequence>
<xs:element name="value" type="xs:float" maxOccurs="&SomeMagicNumber;" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Example Delphi Win32 code to load the schema:
var
XmlSchemas: IXMLDOMSchemaCollection;
XmlSchema: IXMLDOMDocument2;
XmlDocument: IXMLDOMDocument2;
begin
XmlSchemas := CoXMLSchemaCache40.Create;
XmlSchema := CoDOMDocument40.Create;
XmlSchema.load((*INSERT SCHEMA PATH HERE*));
Assert(XmlSchema.parseError.errorCode = 0, XmlSchema.parseError.reason);
XmlSchemas.add((*INSERT SCHEMA TARGET NAMESPACE HERE*), XmlSchema);
XmlDocument := CoDOMDocument40.Create;
XmlDocument.schemas := XmlSchemas;
XmlDocument.validateOnParse := True;
end;
The code asserts after attempting to load the schema. Reason: 'The name of the top most element must match the name of the DOCTYPE declaration.'