Packetizer Logo
 

XML Schema & Versioning

XML Schema is a W3C standard for describing the proper construction of an XML document. There are many books and Internet resources on the topic, so we will no cover the same material here. Nonetheless, we will recommend the book XML Schema by Eric van de Vlist as an excellent source of information on the topic, and the two standards from the W3C: Part 1: Structures and Part 2: Datatypes.

The primary purpose for this page is to discuss how to use XML Schema in order to build XML files that properly support the versioning techniques necessary to implement protocols and complex enterprise systems that utilize XML.

When using XML Schema 1.0, the first recommendation we can make is to not validate the XML file when it is processed. After all, a version 1 system will most certainly see elements and attributes not defined until version 2 of the system was defined. Those unknown elements and attributes must be safely discarded.

It is also recommended that one should not use xs:sequence. Rather, use xs:all. This allows elements to appear in any order, which is very useful when the remote system does not have the means of controlling the order. There are valid reasons for why a system might not know the proper order of elements, such as when messages are transformed by a JSON to XML converter that is ignorant of the underlying schema.

To put it bluntly, XML Schema 1.0 does not provide very much support for building extensible XML-based programmatic interfaces. If a device transmits an XML document that is "version 3" of a given interface to a device that only understands "version 1", there is little in XML Schema 1.0 to help support such exchanges.

Recognizing this is an issue, the W3C XML Schema working group added a number of enhancements to XML Schema 1.1 to better support versioning. While this article is somewhat dated, it is nonetheless useful: Guide to Versioning XML Languages using new XML Schema 1.1 features.

In order to allow any element to appear within any given xs:sequence or xs:all group, XML Schema 1.1 introduces this syntax:

Source Code

<xs:openContent mode="interleave">
  <xs:any namespace="##any" processContents="lax" />
</xs:openContent>

The above syntax may be placed just after xs:complexType, as shown in the versioning document. It may also be placed at the start of the XML Schema in order to indicate that it applies to the entire document. In that case, there is a slight change in that "xs:openContent" is changed to "xs:defaultOpenContent".

One must also ensure that any attribute may be provided as a part of a given complexType. To do that, one should add " " to the end of an complexType definition.

Another requirement one must consider when creating versioned XML interfaces is to ensure that enumerations are extensible. Nearly all enumerations are subject to additions in a future protocol release. Therefore, it is recommended to employ syntax similar to the following:

Source Code

<xs:simpleType name="RejectionReasonEnum">
  <xs:restriction base="xs:token">
    <xs:enumeration value="PermissionDenied">
    <xs:enumeration value="NotSupported">
  </xs:restriction>
</cs:simpleType>

<xs:simpleType name="RejectionReason">
  <xs:union memberTypes="RejectionReasonEnum xs:token">
</xs:simpleType>

It is important to note that most XML data binding tools do not support the above syntax, even though it was legal in XML Schema 1.0. However, it is believed that, given the emphasis on versioning in XML Schema 1.1 and the clear need to change the way in which we view and process XML documents in a heterogeneous network, such syntax must and will be supported.