Thursday, July 13, 2017

XML Schema 1.1: inheritable attributes in action

I felt like writing a small and complete example, clarifying the role of XML Schema 1.1 inheritable attributes (please see inheritable = boolean within the definition "XML Representation Summary: attribute Element Information Item").

The XSD 1.1 inheritable attributes, are primarily useful while implementing Type Alternatives.

Below is a specific XSD 1.1 example, and two corresponding valid XML documents. These as a whole implement inheritable attributes, and few other areas of XSD 1.1.

XSD document:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
   <xs:element name="X">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="Y">
               <xs:alternative test="@y = 'A'">
                  <xs:complexType>
                     <xs:sequence>
                        <xs:element name="A">
                           <xs:complexType>
                              <xs:simpleContent>
                                 <xs:extension base="xs:string">
                                    <xs:attribute name="attr" type="xs:integer"/>
                                 </xs:extension>
                              </xs:simpleContent>                              
                           </xs:complexType>
                        </xs:element>
                     </xs:sequence>
                  </xs:complexType>
               </xs:alternative>
               <xs:alternative test="@y = 'B'">
         <xs:complexType>
                     <xs:sequence>
                        <xs:element name="B">
                           <xs:complexType>
                              <xs:simpleContent>
                                 <xs:extension base="xs:string">
                                    <xs:attribute name="attr" type="xs:integer"/>
                                 </xs:extension>
                              </xs:simpleContent>                              
                           </xs:complexType>
                        </xs:element>
                     </xs:sequence>
                  </xs:complexType>              
               </xs:alternative>
            </xs:element>
         </xs:sequence>
         <xs:attribute name="y" inheritable="true">
            <xs:simpleType>
               <xs:restriction base="xs:string">
                 <xs:enumeration value="A"/>
                 <xs:enumeration value="B"/>
               </xs:restriction>
            </xs:simpleType>
         </xs:attribute>
      </xs:complexType>
   </xs:element>
 
</xs:schema>

(the most interesting parts are shown italicized)

XML document 1:

<?xml version="1.0"?>
<X y="A">
  <Y>
    <A attr="1">hello</A>
  </Y>
</X>

XML document 2:

<?xml version="1.0"?>
<X y="B">
  <Y>
    <B attr="1">hello</B>
  </Y>
</X>

The XML documents shown (1 & 2) should be validated with the XSD document provided.

I'll provide a little explanation here, with-respect-to the semantics of this example:
An inheritable attribute "y" is defined on element "X". The XSD type of element "Y" is chosen at runtime (i.e at validation time), depending on the value of this attribute. Notice how, the attribute defined on element "X" makes its definition accessible to element "Y" in the schema document.

If you're a novice to XML Schema 1.1, I'd suggest reading some areas of the XML Schema 1.1 language more deeply.

I do hope, that this post is useful to some practitioners in this field.

No comments: