An XML Schema describes the structure and content of document types in XML. The real power of XML schemas, however, is that they enable organizations to define and share a common vocabulary on which to base their documents and scripts.
XML Schemas also help in automatically validating the structure and content of the data entered - and as in most business application about 60% of all code is concerned with data validation this is a great saving.
This topic focuses on highlighting the structure of an XML Schema, giving some idea of how the basic model can be extended and some best practices
The structure of the XML Schema will be discussed in stages and finally a complete Schema will be shown. Remember Schemas define:
This topic covers:
Some of these items have been covered in >XML Basics and will only be highlighted here. More detail on the data types is provided in XML Data Types |
An XML Schema starts with an XML Type (document) Declaration, but more importantly is the schema declaration - which defines namespaces, sets various data transparency parameters, etc. - essentially it declares the environment in which the schema will be effective.
Format
<schema |
Schema is the root element of the schema definition The id attribute uniquely identifies this schema The namespaces used in this schema are listed The target namespace is set - elements without an explicit namespace link will use this namespace The transparency (hidden / exposed) of the attribute names is set - whether they will be qualified (showing the namespace information) or not. Defaults to unqualified The transparency (hidden / exposed) of the element names is set - whether they will be qualified (showing the namespace information) or not. Defaults to unqualified The language of the document content (usually ISO 639 Language Code) |
Example
<schema id="camera" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:nikon="http://www.nikon.com"
xmlns:olympus="http://www.olympus.com"
xmlns:pentax="http://www.pentax.com"
xmlns=http://www.camera.org"
targetNamespace="http://www.camera.org"
attributeFormDefault="unqualified"
elementFormDefault="unqualified"
language="en">
Best Practice
attributeFormDefault="unqualified"
and elementFormDefault="unqualified"
) within the Schema when:<chapter:body>
versus <human:body>
xlmns=http://www.camera.org
Any project typically has more than one schema. Schema files can be integrated in three ways:
Format
<include id=ID schemaLocation=anyURI /> <import id=ID namespace=anyURI <redefine id=ID schemaLocation=anyURI /> |
Include - includes external schemas with the same Namespace Import - imports external schemas maintaining different Namespaces Redefine - includes an external schema with the possibility of modification |
Example
<xsd:include schemaLocation="nikon.xsd"/>
<xsd:redefine schemaLocation="olympus.xsd" />
<xsd:import namespace="http://www.pentax.com" schemaLocation="pentax.xsd"/>
Best Practice
<import />
; maintains the integrity of element names; does not allow the components to be modified<include />
or <redefine />
; does not provide any visual indication of the origin or lineage of components and runs the risk of name collisions; allows components to be modified<include />
or <redefine />
but if proxy schemas are employed uses <import />
to integrate the proxy schemas and so avoids name collisions; components can still be customized; part of the lineage of the components remains hidden Type and Element declarations form the heart of any schema. The basic format and options for type and element declarations are covered here.
Defining elements is discussed in XML Basics; Simple and Complex Type Declarations are also covered in XML Data Types |
Format
<--Basic declaration--> <declarationPrefix:simpleType <--Declaration of a restriction--> <declarationPrefix:restriction base=QName> <--Declaration of a list--> <declarationPrefix:list base=QName> <--Declaration of a union--> <declarationPrefix:union base=QName> |
The basic simpleType declaration contains three attributes:
The type information of the data type being defined makes use of basic data types supported by XML see XML Data Types The basic data types are combined using restriction, list or union operators The constraint parameters for the restriction operation are highlighted in XML Data Types |
Examples
<!--simpleType Restriction-->
<!--Bounded Numbers-->
<xsd:simpleType name="theAnswer">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="42" />
<xsd:maxInclusive value="42" />
</xsd:restriction>
</xsd:simpleType>
<!--String Length-->
<xsd:simpleType name="licensePlate">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1" />
<xsd:maxLength value="9" />
</xsd:restriction>
</xsd:simpleType>
<!--Enumeration-->
<xsd:simpleType name="title">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Dr" />
<xsd:enumeration value="Mr" />
<xsd:enumeration value="Mrs" />
<xsd:enumeration value="Ms" />
<xsd:enumeration value="Prof" />
</xsd:restriction base="xsd:string">
</xsd:simpleType>
<!--Digital Numbers-->
<xsd:simpleType name="currency" >
<xsd:restriction base="xsd:decimal">
<xsd:fractionDigits value="2" />
</xsd:restriction>
</xsd:simpleType>
<!--Pattern-->
<xsd:simpleType name="socialSecurityNumber" >
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{3}-[0-9]{2}-
[0-9]{4}" />
</xsd:restriction>
</xsd:simpleType>
<!--Whitespace-->
<xsd:simpleType name="token" >
<xsd:restriction base="xsd:normalizedString">
<xsd:whitespace value="collapse" />
</xsd:restriction>
</xsd:simpleType>
<!--simpleType List-->
<xsd:simpleType name="ages" >
<xsd:list itemType="xsd:positiveInteger">
</xsd:list>
</xsd:simpleType>
<!--simpleType Union-->
<!--Define the base simpleType data sets-->
<xsd:simpleType name="catBreeds">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Abyssinian" />
<xsd:enumeration value="Siamese" />
<xsd:enumeration value="Himalayan" />
<xsd:enumeration value="Persian" />
</xsd:restriction base="xsd:string">
</xsd:simpleType>
<xsd:simpleType name="dogBreeds">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Labrador" />
<xsd:enumeration value="Spaniel" />
<xsd:enumeration value="Terrier" />
<xsd:enumeration value="Poodle" />
</xsd:restriction base="xsd:string">
</xsd:simpleType>
<!--Combine the data sets-->
<xsd:simpleType name="pet">
<xsd:union memberTypes="target:catBreeds
target:dogBreeds" />
</xsd:simpleType>
Format
<--Basic declaration--> <declarationPrefix:complexType <--Simple Content--> <declarationPrefix:simpleContent id=ID> <--Restriction--> <declarationPrefix:restriction id=ID base=QName> <--Extension--> <declarationPrefix:extension id=ID base=QName> <--Complex Content--> <declarationPrefix:complexContent id=ID mixed=boolean> <--Restriction--> <declarationPrefix:restriction id=ID base=QName> <--Extension--> <declarationPrefix:extension id=ID base=QName> |
complexType data types can contain both element content and attributes The complexType declaration contains five attributes:
The content of the data type definition can be:
In addition, the content of the definition can specified by combining elements using:
The base definition of the complexType is can then be further refined by defining:
Finally the complexType content contains attributes, which could simply be listed as individual elements or grouped together in an attributeGroup |
Example
<!--complexType Definition Example-->
<xsd:element name="Catalog">
<--ComplexType Declaration-->
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author">
<xsd:complexType>
<xsd:attribute name="idref" type="xsd:IDREF" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Format
<xsd:element id = ID |
The element declaration contains the following attributes:
|
The content of the element can be of a simpleType or complexType, additionally the element can be specified as:
Example
<xsd:element name = "Customer">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "FirstName" type = "xs:string" />
<xsd:element name = "MiddleInitial" type = "xs:string" />
<xsd:element name = "LastName" type = "xs:string" />
</xsd:sequence>
<xsd:attribute name = "customerID" type = "xs:string" />
</xsd:complexType>
</xsd:element>
There are numerous preferences in arranging the content of the data definition in a schema, some important issues to consider are:
For more details see the xml-dev group best practice recommendations on global versus local declarations. All best practices have been adapted from recommendation put forward by the xml-dev group see http://www.xfront.com/BestPracticesHomepage.html (accessed September 2005) for more details |
Format
<xsd:attribute id = ID |
The attribute declaration is very similar to the element declaration and contains the following attributes:
|
Example
<attribute name="version" type="string" fixed="1.0"/>
<attribute name="name" type="string" use="required"/>
For more details on XML schema structures www.xml.dvint.com/docs/SchemaStructuresQR-2.pdf