XML Schema Introduction

XML Schemas are generally fairly long and complex documents describing a variety of different content model components. However, in the XML Schema primer, the W3C supplies a number of different schemas with the corresponding XML document for addressing common issues. For the sake of brevity, we will examine a portion the first example in this document, PO.xsd, to give a general idea of XML Schema syntax. The links below will lead you to the complete source files, and many more XML Schema examples can be found online.

In addition to providing readers with valuable information about XML Schema and XML Schema resources, this site points readers to a variety of useful tools for working with XML Schema in a range of different environments. These tools include: an XML Schema editor , XML mapping tool , XML Schema modeling in UML, XML Schema file and relationship management, and a XML data binding utility.

The Anatomy of an XML Schema

An XML Schema is comprised of a number of different declarations and definitions that serve to restrict data input through the application of a variety of different rules.

Document type declaration

XML Schema is XML, and therefore conforms to the syntax specified in the W3C XML recommendations. This means that XML Schema can be parsed by a standard XML parser, can be accessed programmatically for integration testing and other validation purposes, and also that it is extensible (as demonstrated by standards such as XBRL).

Note The XML document type declaration is not required by XML Schema, though it is inferred by the root element, <schema>.

Namespace declaration

Namespaces provide a context for element and attribute names used within an XML document, allowing architects to build and extend upon XML vocabularies using URIs to ensure the creation of unique data tags.

A namespace declaration is not required in XML Schema, but namespaces can rather be defined inline with element and attribute in the XML instance. For example:

inline xml namespace

Namespaces can play a vital role in any large data integration project or data exchange scenario, where item names can often come into conflict.

Expanded names

Namespaces in XML dictates that once namespaces are declared, they are enforced through the use of expanded names. An expanded name is simply a namespace name (defined in the namespace declaration) combined with a local name to denote an item definition that is unique to the declared namespace. In the example below, <xsd:annotation> tells us that the definition of annotation applies specifically and uniquely to the XML Schema (xsd) vocabulary.

Type definitions

Type definitions (complexType and simpleType) enable developers to build modular data structures and reuse individual content models without rewriting code every time they need to employ the same data syntax. In the example, line 11 defines a complexType "USAddress", which uses a familiar data structure. This structure is then reused in lines 13 and 14 to describe our shipping and billing addresses, which may contain different content, but will still adhere to the same syntax rules.

Element / Attribute declarations

Element and attribute declarations simply define the names that will be used for tags within the XML instance. Both of these can be further defined with a variety of different constraints including id, type, substitutionGroup, max/minOccurs, etc.

Sequence definition

The sequence element defines the order in which child elements are required to appear in the corresponding XML document.

xml schema example

An easily-interpreted visual model of the same XML Schema is shown below in XMLSpy's graphical schema editor.

xml schema example

Industry Standards Library

The race to standardization has led to the development of a large number of publically available XML Schemas for cross-industry data integration. These schemas specify common vocabularies and data structures to facilitate streamlined information exchange across disparate systems. Altova provides an extensive XML Schema library with links to many of these schemas.