Skip to content

XML Schemas in Nutshell

September 3, 2010

If you are working on web services, you should know many bits and pieces of XML schemas. XML Schema was approved as a W3C Recommendation in May, 2001 and is now being widely used for structuring XML documents for e-commerce and Web Services applications.

XML Schema Overview

The two major goals that the W3C XML Schema working group focused on during the design of the XML Schema standard were:

  • Expressing Object Oriented design principles found in common OO programming languages into the specification.
  • Providing rich datatyping support similar to the datatyping functionality available in most relational database systems.

XML Schemas provides a means of creating a set of rules that can be used to identify document rules governing the validity of the XML documents that you create. Schemas provide a means of defining the structure, content, and semantics of XML documents that can be shared between different types of computers and documents.

Now lets talk about different elements we use to create an XML schema.

Root Element

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ac="http://www.agileconsultants.pk/schemas" targetNamespace="http://www.agileconsultants.pk/schemas" attributeFormDefault="qualified" elementFormDefault="qualified">
<!-- Additional schema contents. --> 
</schema>

The root element of an XML shcema is always the schema element. The default namespace for XML schema is always be the “http://www.w3.org/2001/XMLSchema&#8221;. Default namespace can be used to access the default data types such as string, int, etc. Custom namespace can be defined in the declaration such as “ac” is defined in above example, new types created in the schema can refer to this namespace. attributeFormDefault and elementFormDefault attributes are used to specify that the attributes and elements will be qualified or unqualified. Default value is unqualified.

Elements

Element declarations are used to define elements of an XML document in the Schema. There can be different type of elements in schema such as global, simpleType and complexType. Global elements are the direct children of the XML schema. An element declaration can contain simple or complex type if no type attribute is specified for it. XML schema can also define global attributes which can later be used in schema declarations.

There are number of attributes which can be used with elements. Following are brief description of these attributes:

  • name: Used to specify name of the element
  • type: Used to specify the data type of the element. This attribute cannot be used if element contains a simpleType or complextType elements.
  • default: Used to specify the default value of the element.
  • minOccurs: Used to specify the minimum count of occurance for this element in the XML document.
  • maxOccurs: Used to specify the maximum count of occurance of the element in the XML document. This attribute can be set to a special value “unbounded”.
  • nillable: can the element contain the nil/null value
<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:ac="http://www.agileconsultants.pk/schemas" targetNamespace="http://www.agileconsultants.pk/schemas">

<!-- This is a declaration of a global element which makes it possible to use the element in the root of an XML document (instance).
--> 
<element name="person" type="ac:Person"/>

<complexType name="Person"> 
        <sequence>
               <element name="firstName" type="string" nillable="false"/> 
               <element name="lastName" type="string" nillable="false"/> 
               <element name="age" type="int"/> 
               <element name="favColour" type="string" minOccurs="0" maxOccurs="unbounded"/> 
        </sequence>
</complexType> 

</schema>

Complex Types

Complex types describe how elements are organized in an element. Complex types represent the objects in your XML documents and can use the following elements to organize the properties of the objects:

  • sequence: element in complexType is used to specify that child elements of complexType should be provided in the same order as defined by the sequence element.
  • all: element in complexType is used to specify that child elements of complexType can occur in any order. Only simple elements can occur in all group and it cannot be nested with simple, complex, sequence or all elements.
  • choice: element is used to specify that one element should be provided among the elements mentioned in the choice element.
  • simpleContent: element is used to add attributes to global simple types. This can be used to extend or restrict attributes on other complex types with simple content.

Simple Types

SimpleType element is used to restrict bult-in simple types such as int, string, etc. Following is an example of a simpleType:

<!-- Define a new simple type used for a person's age. This type cannot be further extended (final="#all"), has to have a value in the range from 0 to 150. White-space in values of the age type will be collapsed, i.e. TAB, C/R and LF will be replaced with space, two or more continuous occurrences of spaces will be replaced with one single space, finally heading and trailing white-space will be removed.
--> 
<simpleType name="age" final="#all">
      <restriction base="integer"> 
            <minInclusive value="0"/>
            <maxInclusive value="150"/>
            <whiteSpace value="collapse"/>
      </restriction>
 </simpleType>

The elements contained in the element are called facets. A facet represents a characteristic of the built-in type that is restricted. Please refer to the XML Schema specification for a complete list of which facets are available for the different built-in types.

Facets

This section will show examples of how to use some common facets when defining simple types. Numerical factes are used in the above example. Let’s see some of other facets:

Pattern and Length

The pattern facet allows for supplying a regular expression that the value of the simple type must match. Notice that the pattern facet is not only available for the string built-in type, but also for numeric types like float etc.

Enumeration Facet

Enumeration facet is used to provide a list of values among which a selection should be made:

<simpleType name="colour">
     <restriction base="string">
          <enumeration value="Blue"/> 
          <enumeration value="Green"/>
          <enumeration value="Pink"/>
          <!-- Etc. -->
     </restriction> 
</simpleType>

Import and Include

Existing XML schema definitions can be imported or included in a new XML schema, in order to reuse previous definition and/or to split XML schema definitions into multiple files.

Import: Import a XML schema that has a different namespace from the XML schema into which it is imported.

Include: Include a XML schema that has the same namespace that the XML schema into which it is included.

Advertisements

From → Java, Programming, XML

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: