What Is An XML Schema?
What Is An XML Schema?
XML Schema
Abstract
DTDs are very useful if you want to describe the structure of a document. They have been used to do this for well over
a decade. Unfortunately, they come up short when it comes to the needs of XML application developers. Due to the
fact that XML is used, and will continue to be used, to create data-intensive applications, XML is not used to simply
markup a document--it's used to markup data.
DTD is CFG for document type. It lacks Data types, Inheritance, and Default values. Also, it is Non-XML.
XML Schemas express shared vocabularies and allow machines to carry out rules made by people. They provide a
means for defining the structure, content and semantics of XML documents. XML Schema is a language for defining
the structure of XML documents.
A XML schema describes an XML markup language. Specifically it defines which elements and attributes are used in a
markup language, how they are ordered and nested, and what their data types are. The XML specification includes the
Document Type Definition (DTD), which can be used to describe XML markup languages and to validate XML
documents. While DTDs have proven very useful over the years, they are limited. The W3C created a new way to
describe markup languages called XML schema.
XML schema is an XML based alternative to DTD. An XML schema not only describes the structure of an XML
document, but also address data typing.
XML schema is an alternative and update to DTD. The main objectives of XML schema include:
<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
1 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
<x xmlns="http://www.curt.org/comment.xsd">
<comment>This is a great comment!</comment>
</x>
An XML schema document can play the same role for an XML document as an external DTD. It is included into a
document to be validated using the XML schema model. To reference an XML schema document from an XML
document, add two XML Infoset attributes to the document element of the XML document:
1. The first attribute declares the namespace for XMLSchema-instance. The prefix is normally xsi. The value of the
attribute is fixed.
2. The second attribute specifies the location of the XML schema document. In the simplest case, there is no
namespace associated with this location. Therefore the value of the attribute is the file location relative to the
current XML file.
At the start of your schema you need to place a few lines of code, known as a Prolog, which defines the markup that
follows as a schema, and also defines what type of schema it is. The prolog comes immediately after the XML
declaration, which is the first line of code (after any comments).
The Xerces project provides an example of an XML document that refers to an XML schema document. The document
element of the XML document is personnel:
Like any XML document, an XML schema document contains one document element. Its name is <schema>. Like a
DTD, a schema can contain many different kinds of statements, but the most common ones are:
The attribute list declarations, which are a common part of DTDs, can be in one of two places in an XML schema
document: embedded within an element declaration, or as a standalone group, which will be referenced within one or
more element declarations.
2 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
We think that very soon XML schemas will be used in Web applications as a replacement for DTDs. Here are the
reasons why:
Neither instances nor schemas need to exist as documents. They could be any of:
Elements begin with xsd: to associate them with XML schema namespace through declaration.
<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
where the xsd prefix identifies elements as part of the XML schema language
http://www.w3.org/TR/xmlschema-1/Structures
http://www.w3.org/TR/xmlschema-2/Datatypes
A schema describes the structure of an XML document in terms of complex types and simple types. Complex types
describe how elements are organized and nested. Simple types are the primitive data types contained by elements
and attributes.
One of the benefits of using schema is the ability to control data types for elements and attributes. By allowing
developers to dictate the type of data an element or attribute can contain, schema are much more suited for XML
application development.
3 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
Simple Types
The XML schema sepcification defines many stardard smiple types, called built-in types. These built-in types are the
standard building blocks of an XML schema document. Simple types cannot be broken down into constituent parts.
In other words, a siimple element type will not contain other elements, it will contain only data. Attributes must be
Simple Types. Simple type elements cannot themselves have attributes or contain other elements.
You can derive new simple types types from existing types by restricting the type to a subset of its normal values. An
xsd:simpleType element defines the restricted type. The name attribute of xsd:simpleType assigns a name to the
new type. An xsd:restriction child element specifies what type is being restricted via its base attribute. Facet
children of xsd:restriction specify the constraints on the type. For example, this xsd:simpleType element defines a
independentYear as any year from 1776 on:
<xsd:simpleType name="independentYear">
<xsd:restriction base="xsd:gYear">
<xsd:minInclusive value="1776"/>
</xsd:restriction>
</xsd:simpleType>
There is another more detailed example later under "create your own data type".
Complex Types
A schema may declare complex types, which define how elements that contains other elements are organized.
Complex types allow elements in their content and may carry attributes. For example, the USAddress schema type
defines a US postal address, which contains name, street, city, state and zip:
<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsxd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>
As in the above USAddress example, most complexType declarations in schemas contains a sequence element that
lists one or more elements definitions. The sequence element describes which elements are nested in the
complexType, the type of each nested elements, and the order of the nested elements.
In a complexType, the number of times of an element occurs in an XML document is controlled by the maxOccurs
and minOccurs attributes. For example, in the above USAddress definition, the street element can occur at least one
time and no more than two times:
<xsd:complexType name="USAddress">
4 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
minOccurs="1" maxOccurs="2" / >
<xsxd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>
Note that the default value for both maxOccurs and minOccurs is "1". So if there attributes are omitted, the element
must be present exactly once. If an element is optional, set the minOccurs to "0". If an element can occur any
number of times, set minOccurs="0" and maxOccurs="unbounded". In other words:
Besides the sequence element, the all element is also used in the ComplexType definition. Unlike the sequence
element, which defines the exact order of child elements, the XML schema all element allows the elements in it to
appear in any order. Each element in an all group may occur once or not at all; no other multiplicity is allowed. In
other words, in the all group, the minOccurs is either "0" or "1", the default is "1", the maxOccurs is always "1".
Note: Only single elements may be used in an all group, it can't include other groupings like sequence or all. The
following is the USAddress example that is defined using the all element:
Note: that the names of XML schema types are case-sensitive. When an element declares that it is of a particular
type, it must specify both the namespace and the name of that type exactly as the type declares them.
Use the simpleType element when you want to create a new type that is a refinement of a built-in type (string,
5 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
<xsd:simpleType name="TelephoneNumber">
? The string must follow the pattern: ddd-dddd, where 'd' represents a 'digit'?
??? </xsd:restriction>
</xsd:simpleType>??????
Obviously, in this example the regular expression makes the length facet redundant
The following example is creates a new type called US-Flag-Colors. An element declared to be of this type must have
either the value red, or white, or blue:
<xsd:simpleType name="US-Flag-Colors">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="red"/>
<xsd:enumeration value="white"/>
<xsd:enumeration value="blue"/>
</xsd:restriction>
</xsd:simpleType>
6 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
We can create a simpleType using one of the built-in datatypes as our base type. However, we can create a
simpleType that uses another simpleType as the base.
<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
<xsd:element name="person" type="PersonType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
7 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
<complexType name="Address">
<sequence>
<element name="name" type="string"/>
<element name="street" type="string"/>
<element name="city" type="string"/>
</sequence>
</complexType>
<complexType name="USAddress">
<complexContent>
<extension base="ipo:Address">
<sequence>
<element name="state" type="ipo:USState"/>
<element name="zip" type="positiveInteger"/>
</sequence>
</extension>
</complexContent>
</complexType>
Declarations
Element Declarations
Elements declarations were mentioned in the Data types section. Those elements are declared as children of a
complex type. For example:
<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsxd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
use="fixed" value="US"/>
</xsd:complexType>
These elements in the example can only be referred to inside the complex type USAddress. In addition to declaraing
elements inside a complex type, a schema may also declare global elements, which XML documents can refer to
directly. Global elements are declared as direct children of the schema. In the following example, both the person and
the comment elements are global:
<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
<xsd:element name="person" type="PersonType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
8 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
Note: An element declaration can have a type attribute, or a complexType child element, but it cannot have both a
type attribute and a complexType child element. The following example is not allowed:
Attribute Declarations
Attributes declarations, like element declarations, are expressed as elements in an XML schema. The following
example shows attributes declarations:
To define an attribute:
Note: An attribute may also have occurrence constraints, but they are different from those of elements. Instead of
maxOccurs and minOccurs, attribute types declare the use occurrence constraint, which may be "required",
"optional", or "prohibited", indicating that the attribute must, may, or may not be used, respectively. The default is
"optional". An attribute may also be declared as "fixed": A fixed value is assigned to the attribute no matter what
value appears in the XML instance document.
An attribute may also have a default value, to the assigned if no value is explicitly declared in the XML document. But
the default attribute can be used only when the use attribute is "optional". It doesn't make sense when the use is
"required" or "prohibited". For if it is "required", the attribute must appear in the XML document. If it is "prohibited",
the attribute is not allowed in the XML document.
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
9 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
<xsd:choice>
<xsd:group ref="shipAndBill" />
<xsd:element name="singleUSAddress" type=" USAddress" />
</xsd:choice>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items" />
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date" />
</xsd:complexType>
<xsd:group name="shipAndBill">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress" />
<xsd:element name="billTo" type="USAddress" />
</xsd:sequence>
</xsd:group>
The advantage of defining USAddress 's element declarations and wrapping them in a named type is that now this
type can be reused by other elements.
10 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
is equivalent to:
<xsd:element name="A">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="B" .../>
<xsd:element name="C" .../>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<xsd:complexType name="order">
</xsd:complexType>
<xsd:complexType name="shipAddress">
</xsd:complexType>
<xsd:complexType name="cdItems">
</xsd:complexType>
<xsd:complexType name="cdItem">
<xsd:element name="quantity"
type="xsd:positiveInteger"/>
</xsd:complexType>
</xsd:schema>
The XML schema above defines the element <shipOrder> to be of the type order.
The order is a complex type element consisting of the elements <shipTo> and <items>.
The <shipTo> element is of the type shipAddress - a complex type element consisting of the elements <name>,
<street>, <address>, and <country>.
The <items> element is of the type cdItems - a complex type element consisting of <item> elements.
11 of 12 08/12/2008 22:43
XML Schema http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_sc...
The <item> element is of the type cdItem - a complex type element consisting of <title>, <quantity>, and <price>
elements.
<?xml version="1.0"?>
<shipOrder>
<shipTo>
<name>Tove Svendson</name>
<street>Ragnhildvei 2</street>
<address>4000 Stavanger</address>
<country>Norway</country>
</shipTo>
<items>
<item>
<title>Empire Burlesque</title>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<quantity>1</quantity>
<price>9.90</price>
</item>
</items>
</shipOrder>
The XML document consists of a root element <shipOrder>, with two child elements <shipTo> and <items>.
The <items> element contains <item> elements. An <item> element contains <title>,<quantity>, and <price>
elements.
12 of 12 08/12/2008 22:43