Using Entity Constants in an XML Schema

I am trying to create an XML schema in which many types use some "magic numbers".

Instead of changing my scheme in several places if / when these magic numbers change, I would like to put them in some kind of constant definition.

I played with adding DTDs to my schema and declaring some objects here. But I am by no means a DTD specialist, and although it works in a C # application using a scheme, there is also a native Win32 application that uses the same scheme with msxml 4.0 where it explodes ...

Does anyone have experience expanding the definition of a schema this way (can this be done), or is there a better way?

(EDIT: example)

XML example:

<?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> 

Delphi Win32 sample code for loading a 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 after trying to load the circuit is confirmed. Reason: 'The name of the top element must match the name of the DOCTYPE declaration.'

+4
source share
3 answers

Yes, you can use entities to define constants in an XML Schema file.

The code is confirmed after trying to load the circuit. Reason: "The name of the top element must match the name of the DOCTYPE declaration.

 <?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"> ...(clipped away) </xs:schema> 

Your problem is that DTDs are not a namespace. Because of this, the analyzer sees a DTD that defines the root <schema> element, while your document has the root element <xs:schema> . Instead, try using <!DOCTYPE xs:schema [ . This "hardcoding" prefix may seem erroneous, but in DTD there is no simple general way to display a namespace prefix.

If you use the same โ€œmagic numbersโ€ in several schemes, you can also define objects in a separate DTD, and then turn them on by accessing it through a parameter object in the built-in DTD.

 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE xs:schema [ <!ENTITY % magicNumbers SYSTEM "url/to/your/entity/dtd-document"> %magicNumbers; ]> <xs:schema ... > 
+2
source

The W3C XSD schema is an XML document, therefore entities are allowed and supported. When a Schema file is read and processed, entities will be expanded to create XML information.

http://www.xml.com/pub/a/2002/02/27/q-and-a.html

By the way, XSD itself is an XML document, of course, so nothing prevents you from using objects in the Scheme itself. (This is a bit flawed, requiring a DTD Schema to declare these organizations.) You simply cannot use an XML schema to declare objects for use in other documents.

Entities can be a convenient way to avoid copy / paste and simplify the maintenance of XML instance files.

If it explodes in a native Win32 application when it analyzes a circuit, it sounds like an error in MSXML 4.0 or a native Win32 application.

+2
source

You can create a generic xsd and import it from other schemes. See Import Types .

The report.xsd report schema uses the simple xipo: SKU type, which is defined in a different schema and in a different target namespace.

 <import namespace="http://www.example.com/IPO"/> 
0
source

All Articles