Sunday, December 18, 2016

XML Schema 1.1 type alternative patterns


Please note that, XML Schema 1.1 type alternatives allows selection of an XML Schema type depending on something related to attributes.

I think use of XML Schema 1.1 type alternatives fall into the following 2 broad patterns:

1) When an attribute is mandatory

The following XML Schema document illustrates this use:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="X">
      <xs:alternative test="@a = 'val1'" type="XType1"/>
      <xs:alternative test="@a = 'val2'" type="XType2"/>
      <xs:alternative type="XType3"/>
   </xs:element>
  
   <xs:complexType name="XType1">
      <xs:sequence>
        <xs:element name="A" type="xs:integer"/>
        <xs:element name="B" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="required"/>
   </xs:complexType>
  
   <xs:complexType name="XType2">
      <xs:sequence>
         <xs:element name="C" type="xs:integer"/>
         <xs:element name="D" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="required"/>
   </xs:complexType>
  
   <xs:complexType name="XType3">
      <xs:sequence>
         <xs:element name="P" type="xs:integer"/>
         <xs:element name="Q" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="required"/>
   </xs:complexType>
 
</xs:schema>

Following are some of XML instance documents, which are valid according to the above schema document.

<X a="val1">
  <A>1</A>
  <B>2</B>
</X>

i.e when attribute's value is "val1", a specific complex type is assigned to X.

<X a="val2">
  <C>1</C>
  <D>2</D>
</X>

i.e when attribute's value is "val2", a specific complex type is assigned to X.

and

<X a="something else">
  <P>1</P>
  <Q>2</Q>
</X>

i.e when attribute's value is anything other than "val1" or "val2", a specific complex type is assigned to X.  

2) When an attribute is optional

The following XML Schema document illustrates this use:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="X">
      <xs:alternative test="@a" type="XType1"/>
      <xs:alternative test="not(@a)" type="XType2"/>
   </xs:element>
  
   <xs:complexType name="XType1">
      <xs:sequence>
        <xs:element name="A" type="xs:integer"/>
        <xs:element name="B" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="optional"/>
   </xs:complexType>
  
   <xs:complexType name="XType2">
      <xs:sequence>
         <xs:element name="C" type="xs:integer"/>
         <xs:element name="D" type="xs:integer"/>
      </xs:sequence>
      <xs:attribute name="a" type="xs:string" use="optional"/>
   </xs:complexType>
 
</xs:schema>

Following are some of XML instance documents, which are valid according to the above schema document.

<X a="something1">
  <A>1</A>
  <B>2</B>
</X>

i.e when an attribute is present, a specific complex type is assigned to X.

and

<X>
  <C>1</C>
  <D>2</D>
</X>

i.e when an attribute is absent, a specific complex type is assigned to X.

I hope that this post is useful.