One of the other features of XML Schema 1.1, that I like very much is "conditional type assignment", or CTA. The only requirement is, that there must be an attribute on an XML element to use this feature.
Here is a very simple example.
I'm directly writing an XML Schema 1.1 document below using CTA:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="X">
<xs:alternative type="Type1" test="@xa = 1"/>
<xs:alternative type="Type2" test="@xa = 2"/>
<xs:alternative type="xs:error"/>
</xs:element>
<xs:complexType name="Type1">
<xs:sequence>
<xs:element name="a" type="xs:int"/>
<xs:element name="b" type="xs:int"/>
</xs:sequence>
<xs:attribute name="xa" type="xs:int"/>
</xs:complexType>
<xs:complexType name="Type2">
<xs:sequence>
<xs:element name="p" type="xs:int"/>
<xs:element name="q" type="xs:int"/>
</xs:sequence>
<xs:attribute name="xa" type="xs:int"/>
</xs:complexType>
</xs:schema>
The requirement of XML Schema 1.1 validation in this case is: If the attribute "xa" on element "X" has value 1, then element "X" has a certain type. If the value of attribute "xa" is 2, then element "X" has another type.
The two valid XML documents for the given XML Schema document are following:
<?xml version="1.0" encoding="UTF-8"?>
<X xa="1">
<a>1</a>
<b>2</b>
</X>
and,
<?xml version="1.0" encoding="UTF-8"?>
<X xa="2">
<p>1</p>
<q>2</q>
</X>
For anything else as value of attribute "xa", or infact any other kind of content the element "X" will be assigned the type xs:error (which makes the element "X" invalid").
No comments:
Post a Comment