Since the introduction of XML Schema 1.0, the standard has been widely adopted. Developers have requested several changes over the years, most notably the ability to specify and validate more granular business rules and also to allow flexible schema evolution. There are numerous changes and enhancements in the XML Schema 1.1 Recommendation aimed at addressing these issues.
Let’s take a look at the most notable new features and some resources for learning more or getting started right away with XML Schema 1.1.
Learn What’s New in XML Schema 1.1
There’s a comprehensive explanation of the new and enhanced features in XML Schema 1.1 in our free, online XML Schema training course, which you can access at any time and complete at your own pace. If you’re an experienced schema developer and simply want to learn about what’s new and enhanced in XSD 1.1, skip ahead to Chapter 9. This course is a great way to learn about XML Schema 1.1 and quickly adapt your schemas to take advantage of the new features.
In this article, I’ll explain several of the highlights and show you how you can implement the new features using XMLSpy. Complete support for XML Schema 1.1 is provided in XMLSpy versions 2014 and higher, including validation of XSD and XML instance files based on XML Schema 1.1, as well as intelligent XSD 1.1 editing tools in the graphical XML Schema 1.1 editor.
Assertions, also called co-constraints, in XML Schema 1.1 allow rules-based validation of business policies involving more than one element. These rules are defined using XPath expressions. For example, you can use the new <assert> element to test if an expense is less than or equal to the permitted amount, a date is with a certain time frame, and so on. In this example, the assertion specifies that the end time of a meeting must be greater than the start time. You can see this represented in XML Schema design view here:
And in the corresponding code:
It’s easy to review the assertions that exist in a schema using the Globals view, which provides an Assertions tab for XML Schema 1.1 documents. You can also add new assertions and select the XPath default namespace here.
Or, define them in the content model view by right clicking an element in the content model view and choosing Add child assertion. You can define the XPath test directly in the content model via the Details entry helper window.
This ability to define and validate business rules is a major advantage of XML Schema 1.1, and this graphical view helps guide you through the process.
Conditional Type Alternatives
Conditional type alternatives are another feature designed to make XSDs more powerful by adding built-in flexibility. These allow a type to be dynamically assigned to an element based on an attribute value in the XML instance document. The xs:alternative element has two attributes: type and test. Here is an example using the Meeting schema we started with above.
If the end-time of the meeting is before noon, the type of the meeting is MorningMeeting. After noon, it’s AfternoonMeeting. To define this in the XML Schema designer, we simply right-click the Meeting element and choose Add Child / Alternative. Use the Details entry helper to enter an XPath expression, and then select the Type that will be dynamically assigned to the element from the Type drop down menu.
Now, any Meeting element in an XML instance with an end time after 12:00:00 will use the MeetingType AfternoonMeeting. Using a graphical XSD editor makes it easy to visualize and configure these relationships.
XML Schema 1.1 allows you to define a set of default attributes that automatically apply to every complex type in the schema. For instance, in our Meeting schema, it is helpful to have the attributes startTime and endTime apply to all elements, without having to add them manually, which is tedious and potentially error-prone – especially in a schema with a large number of elements.
The xs:schema element in XML Schema 1.1 has an optional defaultAttributes element, which identifies an attribute group. (If there is a particular element in which it’s not desirable to apply the default attribute(s), you can specify that with defaultAttributesApply=”false”.)
It’s easy to add an attributeGroup and define the default attributes in the XML Schema editor Globals view.
Then, use the Schema Settings dialog accessed via the Schema design menu to select the attributeGroup you just created as the defaultAttributes from a drop-down list.
Above are some of the major updates provided by XML Schema 1.1. Others include inheritable attributes, enhanced functionality for substitution groups, an xs:error datatype, and others. All of these are explained in detail with real-world example files in chapter 9 of the XML Schema online training course.