Inside XML Schemas
by Thomas Erl

XSD is a comprehensive data modeling language for XML documents, and the one XML schema specification that has received the broadest industry support across contemporary XML and Web services technologies.

Unlike DTDs, the XML Schema Definition Language is an actual implementation of the XML language; schemas are themselves XML documents. XSD provides the structural and validation-related features offered by the DTD language within an extended feature set consisting of many more variations and options in how to model and establish validation criteria for XML document data.

One of the most important features introduced by the XML Schema specification, is the wide range of support for data types. This greatly refines the quality of XML data representation, and further underscores its role as an enterprise data transport standard. XSD schemas also provide support for namespaces. This enables the schema author to establish logical domains to which some or all parts of a schema can be applied.


The XML Schema document format is very flexible and highly extensible. Each schema definition is capable of containing multiple schema documents. This allows for the creation of modular schemas that can be combined or individually processed.







Each schema can be dynamically extended with supplementary constructs. This allows schemas to adapt to different data representation requirements.









Parts of a schema definition can be redefined (overridden) by other schema definitions. This allows you to create a series of schema classes.







The XSD schema is defined using the following header statements:

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Note the use of the xmlns:xsd attribute, which declares the namespace. This value is carried forward throughout the schema by the use of the xsd: prefix.

Schemas can be fully customized, and developers can create and link schemas to their own namespaces, by using custom prefixes.

<xsd:element name="book">
   <xsd:complexType>
     <xsd:sequence>

XML schema elements have either simple or complex types, depending on whether or not they contain attributes or child elements. Since book is a parent element that will have child elements, it has a complex type. The sequence element acts as a compositor, setting up the sequence of the nested child elements.

       <xsd:element name="title" type="xsd:string"/>
       <xsd:element name="author" type="xsd:string"/>
     </xsd:sequence>

Since neither of the title or author elements have attributes or child elements, they have simple types, namely the built-in type xsd:string. The book element, however, does have an attribute, which is declared separately within the complexType construct.

      <xsd:attribute name="category">
         <xsd:simpleType>
           <xsd:restriction base="xsd:string">
             <xsd:enumeration value="Fiction"/>
             <xsd:enumeration value="Non-Fiction"/>
           </xsd:restriction>
         </xsd:simpleType>
      </xsd:attribute>

Note that the use of this attribute also introduces the schema's first validation rule. The category attribute is limited to one of two values, through the use of the restriction element. Also, notice the use of the simpleType element – since XML attributes cannot contain further attributes or child elements, they always have simple types.

The declaration of the book element, (along with its nested child elements and its attribute) is completed by closing off the complexType and element constructs.

   </xsd:complexType>
</xsd:element>

Finally, the schema construct itself is closed:
</xsd:schema>

The entire XSD schema definition appears as follows:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="book">
   <xsd:complexType>
     <xsd:sequence>
       <xsd:element name="title" type="xsd:string"/>
       <xsd:element name="author" type="xsd:string"/>
     </xsd:sequence>
     <xsd:attribute name="category">
       <xsd:simpleType>
         <xsd:restriction base="xsd:string">
           <xsd:enumeration value="Fiction"/>
           <xsd:enumeration value="Non-Fiction"/>
         </xsd:restriction>
       </xsd:simpleType>
     </xsd:attribute>
   </xsd:complexType>
</xsd:element>
</xsd:schema>

This schema can now be implemented using an XML document, such as our familiar example:

<?xml version="1.0"?>
<book xmlns="http://www.xmltc.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="book.xsd"
  category="Fiction">
   <title>Joy of Integration</title>
   <author>Joe Smith</author>
</book>

The above introduction to XSD schemas has only scratched the surface of this technology's capabilities. The XML Schema Definition Language provides a number of advanced content modeling features, including:

•  support for a wide variety of data types
•  advanced validation syntax
•  the ability to simulate relational schema models through the use of constraints and keys
•  extensibility via the any element
•  modularization using the include and import elements
•  a form of validation inheritance using the redefine element

SOA Design Patterns by Thomas Erl
Foreword by Grady Booch
With contributions from David Chappell, Jason Hogg, Anish Karmarkar, Mark Little, David Orchard, Satadru Roy, Thomas Rischbeck, Arnaud Simon, Clemens Utschig, Dennis Wisnosky, and others.
Web Service Contract Design & Versioning for SOA by Thomas Erl, Anish Karmarkar, Priscilla Walmsley, Hugo Haas, Umit Yalcinalp, Canyang Kevin Liu, David Orchard, Andre Tost, James Pasley
SOA Principles of Service Design by Thomas Erl
Service-Oriented Architecture: A Field Guide to Integrating XML and Web Services by Thomas Erl
Service-Oriented Infrastructure:On-Premise and in the Cloud by Raj Balasubramanian, Benjamin Carlyle, Thomas Erl, Cesare Pautasso
Next Generation SOA:A Real-World Guide to Modern Service-Oriented Computing by Pethuru Cheliah, Thomas Erl, Clive Gee, Robert Laird, Berthold Maier, Hajo Normann, Leo Shuster, Bernd Trops, Clemens Utschig, Torsten Winterberg
SOA with .NET & Windows Azure: Realizing Service-Orientation with the Microsoft Platform by David Chou, John deVadoss, Thomas Erl, Nitin Gandhi, Hanu Kommalapati, Brian Loesgen, Christoph Schittko, Herbjorn Wilhelmsen, Mickey Williams
SOA Governance:
Governing Shared Services On-Premise & in the Cloud
by Stephen Bennett, Thomas Erl, Clive Gee, Anne Thomas Manes, Robert Schneider, Leo Shuster, Andre Tost, Chris Venable
SOA with Java by Raj Balasubramanian, David Chou, Thomas Erl, Thomas Plunkett, Satadru Roy, Philip Thomas, Andre Tost
Modern SOA Methodology: Methods for Applying Service-Orientation On-Premise & in the Cloud by Raj Balasubramanian, David Chou, Thomas Erl, Thomas Plunkett, Satadru Roy, Philip Thomas, Andre Tost
Cloud Computing: Concepts, Technology & Architecture by Thomas Erl, Zaigham Mahmood, Ricardo Puttini
Cloud Computing Design Patterns by Thomas Erl, Amin Naserpour

For more information about these books, visit: www.servicetechbooks.com


Arcitura Education Inc.
Arcitura Education Inc. is a leading global provider of progressive, vendor-neutral training and certification programs, providing industry-recognized certification programs for a range of certifications.
For more information:
www.arcitura.com
SOA Certified Professional (SOACP)
The books in this series are part of the official curriculum for the SOA Certified Professional program.
For more information:
www.soaschool.com
Cloud Certified Professional (CCP)
The books in this series are part of the official curriculum for the Cloud Certified Professional program.
For more information:
www.cloudschool.com
Big Data Science Certified Professional (BDSCP)
The books in this series are part of the official curriculum for the Big Data Science Certified Professional program.
For more information:
www.bigdatascienceschool.com/