XML Schema Tutorial
XML Schema Tutorial
Schema Tutorial
In our Schema tutorial, you will learn what an XML Schema is. You will also
learn how XML Schema will replace DTD, and how to use the XML Schema
language in your applications. Start Learning XML Schema!
Schema References
At W3Schools you will find a list of valid XML Schema Elements and Data
Types. XML Schema Elements.
Table of Contents
Before you study the XML Schema Language, you should have a basic understanding of XML and XML
Namespaces. It will also help to have some basic understanding of DTD.
If you want to study these subjects first, please visit our XML tutorial and our DTD tutorial.
The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.
An XML Schema:
We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs.
Here are some reasons:
• XML Schemas are extensible to future additions
• XML Schemas are richer and more useful than DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces
XML Schema was originally proposed by Microsoft, but became an official W3C recommendation in May
2001.
The specification is now stable and has been reviewed by the W3C Membership.
For a full overview of W3C Activities and Status, visit our W3C tutorial.
There are a number of reasons why XML Schema is better than DTD.
One of the greatest strength of XML Schemas is the support for data types.
With the support for data types:
Another great strength about XML Schemas is that they are written in XML.
Because XML Schemas are written in XML:
XML Schemas are extensible, just like XML, because they are written in XML.
With an extensible Schema definition you can:
A well-formed XML document is a document that conforms to the XML syntax rules:
Even if documents are Well-Formed they can still contain errors, and those errors can have serious
consequences. Think of this situation: you order 5 gross of laser printers, instead of 5 laser printers. With XML
Schemas, most of these errors can be caught by your validating software.
XSD How To
A Simple DTD
This is a simple DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Line 1 defines the note element to have four elements: "to, from, heading, body". Line 2-5 defines the to
element to be of the type "#PCDATA", the from element to be of the type "#PCDATA", and so on...
This is a simple XML Schema file called "note.xsd" that defines the elements of the XML document above
("note.xml"):
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The note element is said to be of a complex type because it contains other elements. The other elements (to,
from, heading, body) are said to be simple types because they do not contain other elements. You will learn
more about simple and complex types in the following chapters.
A Reference to a DTD
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
A simple element is an XML element that can contain only text. It cannot contain any other elements or
attributes.
However, the "only text" restriction is quite misleading. The text can be of many different types. It can be one
of the types that are included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom
type that you can define yourself.
You can also add restrictions (facets) to a data type in order to limit its content, and you can require the data
to match a defined pattern.
How to Define a Simple Element
where xxx is the name of the element and yyy is the data type of the element.
Here are some XML elements:
<lastname>Refsnes</lastname>
<age>34</age>
<dateborn>1968-03-27</dateborn>
And here are the corresponding simple element definitions:
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
XML Schema has a lot of built-in data types. Here is a list of the most common types:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
XSD Attributes
Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type.
But the attribute itself is always declared as a simple type. This means that an element with attributes always
has a complex type definition.
where xxx is the name of the attribute and yyy is the data type of the attribute.
Here are an XML element with an attribute:
<lastname lang="EN">Smith</lastname>
And here are a corresponding simple attribute definition:
<xs:attribute name="lang" type="xs:string"/>
XML Schema has a lot of built-in data types. Here is a list of the most common types:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute:
<xs:attribute name="lang" type="xs:string" use="optional"/>
To make an attribute required:
<xs:attribute name="lang" type="xs:string" use="required"/>
Restrictions on Content
When an XML element or attribute has a type defined, it puts a restriction for the element's or attribute's
content. If an XML element is of type "xs:date" and contains a string like "Hello Mother", the element will not
validate.
But, there is more... with XML Schemas, you can add your own restrictions to your XML elements and
attributes. These restrictions are called facets. You can read more about facets in the next chapter.
XSD Restrictions/Facets
Restrictions are used to control acceptable values for XML elements or attributes.
Restrictions on XML elements are called facets.
Restrictions on Values
This example defines an element called "age" with a restriction. The value of age can NOT be lower than 0 or
greater than 100:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
To limit the content of an XML element to a set of acceptable values, we would use the enumeration
constraint.
This example defines an element called "car":
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "car" element is a simple type with a restriction. The acceptable values are: Audi, Golf, BMW.
The example above could also have been written like this:.
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
Note: In this case the type "carType" can be used by other elements because it is not a part of the "car"
element.
To limit the content of an XML element to define a series of numbers or letters that can be used, we would use
the pattern constraint.
This example defines an element called "letter":
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "letter" element is a simple type with a restriction. The only acceptable value is ONE of the LOWERCASE
letters from a to z.
The next example defines an element called "initials":
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "initials" element is a simple type with a restriction. The only acceptable value is THREE of the
UPPERCASE letters from a to z.
This example also defines an element called "initials":
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "initials" element is a simple type with a restriction. The only acceptable value is THREE of the
LOWERCASE OR UPPERCASE letters from a to z.
This example defines an element called "choice":
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "choice" element is a simple type with a restriction. The only acceptable value is ONE of the following
letters: x, y, OR z.
The next example defines an element called "prodid":
<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "prodid" element is a simple type with a restriction. The only acceptable value is FIVE digits in a
sequence, and each digit must be in a range from 0 to 9.
To specify how white space characters should be handled, we would use the whiteSpace constraint.
This example defines an element called "address":
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "address" element is a simple type with a restriction. The whiteSpace constraint is set to "preserve", which
means that the XML processor WILL NOT remove any white space characters.
This example also defines an element called "address":
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This "address" element is a simple type with a restriction. The whiteSpace constraint is set to "replace", which
means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and
carriage returns) with spaces.
This example also defines an element called "address":
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This "address" element is a simple type with a restriction. The whiteSpace constraint is set to "collapse", which
means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage
returns are replaced with spaces, leading and trailing spaces are removed, multiple spaces are reduced to a
single space).
Restrictions on Length
To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.
This example defines an element called "password":
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The "password" element is a simple type with a restriction. The value must be exactly eight characters.
This example defines another element called "password":
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This "password" element is a simple type with a restriction. The value must be minimum five characters and
maximum eight characters.
Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or
greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to
or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this
value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or
equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal
to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than
this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or
equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal
to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) are
handled
A complex element is an XML element that contains other elements and/or attributes.
There are four kinds of complex elements:
• empty elements
• elements that contain only other elements
• elements that contain only text
• elements that contain both other elements and text
Look at this complex XML element, "employee", which contains only other elements:
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
We can define a complex element in an XML Schema in different ways:.
1. The "employee" element can be declared directly by naming the element, like this:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
If you use the method described above, only the "employee" element can use the specified complex type.
Notice that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This
means that the child elements must appear in the same order as they are declared; "firstname" first and
"lastname" second. You will learn more about indicators in the XSD Indicators chapter.
2. The "employee" element can have a type attribute that references to the name of the complex type to use:
<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
If you use the method described above, several elements can refer to the same complex type, like this:
<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
You can also base a complex type element on an existing complex type and add some elements, like this:
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
An empty complex element can contain attributes; but it cannot have any content
between the opening and closing tags.
An "elements only" complex type contains an element that contains only other
elements.
A mixed complex type element can contain attributes, elements, and text.
Define Complex Types with Mixed Content
An XML element, "letter", that contains both other elements and text:
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
Notice the text that appears between the elements. "name", "orderid", and "shipdate" are all children of letter.
The following schema declares the "letter" element:
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Note: To enable character data to appear between the child-elements of letter, the mixed attribute must be set
to true. The <xs:sequence> tag means that the elements defined (name, orderid and shipdate) must appear in
that order inside a "letter" element.
We could also give the complexType element a name, and let the "letter" element have a type attribute that
refers to the name of the complexType (if you use this method, several elements can refer to the same
complex type):
<xs:element name="letter" type="lettertype"/>
<xs:complexType name="lettertype" mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
Indicators
• All
• Choice
• Sequence
Occurrence indicators:
• maxOccurs
• minOccurs
Group indicators:
• Group name
• attributeGroup name
Order Indicators
Occurrence Indicators
Occurrence indicators are used to define how often an element can occur.
Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the
default value for maxOccurs and minOccurs is 1!!!!!
maxOccurs Indicator
The <maxOccurs> indicator specifies the maximum number of times an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The example above indicates that the "child_name" element can minimum occur one time (the default value
for minOccurs is 1) and maximum occur ten times in a "person" element.
minOccurs Indicator
The <minOccurs> indicator specifies the minimum number of times an element can occur:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The example above indicates that the "child_name" element can minimum occur zero times and maximum
occur ten times in a "person" element.
To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:
A working example:
An XML file called "Myfamily.xml":
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="family.xsd">
<person>
<full_name>Hege Refsnes</full_name>
<child_name>Cecilie</child_name>
</person>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name>
<child_name>Stale</child_name>
<child_name>Jim</child_name>
<child_name>Borge</child_name>
</person>
<person>
<full_name>Stale Refsnes</full_name>
</person>
</persons>
The XML file above contains a root element named "persons". Inside this root element are defined several
"person" elements. Each "person" element must contain a "full_name" child element and it can contain up to
five "child_name" child elements.
Here is the schema file "family.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string"
minOccurs="0" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Group Indicators
The <any> element enables us to extend the XML document with elements not
specified by the schema!
The <any> element enables us to extend the XML document with elements not specified by the schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the
"person" element. By using the <any> element we can extend (after <lastname>) the content of "person" with
any element:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Now we want to extend the "person" element with a "children" element. In this case we can do so, even if the
author of the schema above never declared any "children" element!
Look at this schema file, called "children.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="children">
<xs:complexType>
<xs:sequence>
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and
"children.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com children.xsd">
<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
<childname>Cecilie</childname>
</children>
</person>
<person>
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an
optional element after the "lastname" element!
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents
to contain additional elements that are not declared in the main XML schema!
The <anyAttribute> element enables us to extend the XML document with attributes
not specified by the schema!
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the
schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the
"person" element. By using the <anyAttribute> element we can add any number of attributes to the "person"
element:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the
author of the schema above never declared any "gender" attribute!
Look at this schema file, called "attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:attribute name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>
The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and
"attribute.xsd":
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
http://www.w3schools.com attribute.xsd">
<person gender="female">
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
</person>
<person gender="male">
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
</person>
</persons>
The XML file above is valid because the schema "family.xsd" allows us to add an attribute to the "person"
element!
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents
to contain additional elements that are not declared in the main XML schema!
Element Substitution
Let's say that we have users from two different countries: England and Norway. We would like the possibility
to let the user choose whether he or she would like to use the Norwegian element names or the English
element names in the XML document.
To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head
element and then we declare the other elements which state that they are substitutable for the head element.
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
In the example above, the "name" element is the head element and the "navn" element is substitutable for
"name".
Look at this fragment of an XML schema:
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
<xs:complexType name="custinfo">
<xs:sequence>
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
<xs:element name="customer" type="custinfo"/>
<xs:element name="kunde" substitutionGroup="customer"/>
A valid XML document (according to the schema above) could look like this:
<customer>
<name>John Smith</name>
</customer>
or like this:
<kunde>
<navn>John Smith</navn>
</kunde>
To prevent other elements from substituting with a specified element, use the block attribute:
<xs:element name="name" type="xs:string" block="substitution"/>
Look at this fragment of an XML schema:
<xs:element name="name" type="xs:string" block="substitution"/>
<xs:element name="navn" substitutionGroup="name"/>
<xs:complexType name="custinfo">
<xs:sequence>
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
<xs:element name="customer" type="custinfo" block="substitution"/>
<xs:element name="kunde" substitutionGroup="customer"/>
A valid XML document (according to the schema above) looks like this:
<customer>
<name>John Smith</name>
</customer>
BUT THIS IS NO LONGER VALID:
<kunde>
<navn>John Smith</navn>
</kunde>
Using substitutionGroup
The type of the substitutable elements must be the same as, or derived from, the type of the head element. If
the type of the substitutable element is the same as the type of the head element you will not have to specify
the type of the substitutable element.
Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be
declared as global elements, otherwise it will not work!
Global elements are elements that are immediate children of the "schema" element! Local elements are
elements nested within other elements!
An XSD Example
This chapter will demonstrate how to write an XML Schema. You will also learn that a
schema can be written in different ways.
An XML Document
Now we are going to create a schema for the XML document above!
We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow
the structure in the XML document and define each element as we find it. We will start with the standard XML
declaration followed by the xs:schema element that defines a schema:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
...
</xs:schema>
In the schema above we use the standard namespace (xs), and the URI associated with this namespace is
the Schema language definition, which has the standard value of http://www.w3.org/2001/XMLSchema.
Next, we have to define the "shiporder" element. This element has an attribute and it contains other elements,
therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a
xs:sequence element that defines an ordered sequence of sub elements:
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
...
...
</xs:sequence>
...
</xs:complexType>
</xs:element>
Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes
or other elements). The type (xs:string) is prefixed with the namespace prefix associated with XML Schema
that indicates a predefined schema data type:
<xs:element name="orderperson" type="xs:string"/>
Next, we have to define two elements that are of the complex type: "shipto" and "item". We start by defining
the "shipto" element:
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
With schemas we can define the number of possible occurrences for an element with the maxOccurs and
minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and
minOccurs specifies the minimum number of occurrences for an element. The default value for both
maxOccurs and minOccurs is 1!!!!!
Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element.
This is specified by setting the maxOccurs attribute of the "item" element to "unbounded" which means that
there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element
is optional. We have specified this by setting the minOccurs attribute to zero:
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
We can now declare the attribute of the "shiporder" element. Since this is a required attribute we specify
use="required".
Note: The attribute declarations must always come last:
<xs:attribute name="orderid" type="xs:string" use="required"/>
Here is the complete listing of the schema file called "shiporder.xsd":
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
The previous design method is very simple but can be very difficult to read and maintain when documents are
complex!
The next design method is based on defining all elements and attributes first, and then reference to them
using the ref attribute.
Here is the new look of the schema file ("shiporder.xsd"):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
The third design method defines classes or types, that enables us to reuse element definitions. This is done
by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of
the element.
Here is the new look of the schema file ("shiporder.xsd"):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
</xs:simpleType>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="itemtype">
<xs:sequence>
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipordertype">
<xs:sequence>
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
</xs:sequence>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
</xs:complexType>
<xs:element name="shiporder" type="shipordertype"/>
</xs:schema>
The restriction element indicates that the datatype is derived from a W3C XML Schema namespace datatype.
So, this fragment:
<xs:restriction base="xs:string">
means that the value of the element or attribute must be a string value.
The restriction element is more often used to apply restrictions on elements. Look at the following lines from
the schema above:
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
This indicates that the value of the element or attribute must be a string and it must be exactly six characters
in a row and those characters must be a number from 0 to 9.
String data types are used for values that contains character strings.
The string data type can contain characters, line feeds, carriage returns, and tab characters.
The following is an example of a string declaration in a schema:
<xs:element name="customer" type="xs:string"/>
An element in your document might look like this:
<customer>John Smith</customer>
Or it might look like this:
<customer> John Smith </customer>
Note: The XML processor will not modify the value if you use the string data type.
The normalizedString data type is derived from the String data type.
The normalizedString data type also contains characters, but the XML processor will remove line feeds,
carriage returns, and tab characters.
The following is an example of a normalizedString declaration in a schema:
<xs:element name="customer" type="xs:normalizedString"/>
An element in your document might look like this:
<customer>John Smith</customer>
Or it might look like this:
<customer> John Smith </customer>
Note: In the example above the XML processor will replace the tabs with spaces.
The token data type is also derived from the String data type.
The token data type also contains characters, but the XML processor will remove line feeds, carriage returns,
tabs, leading and trailing spaces, and multiple spaces.
The following is an example of a token declaration in a schema:
<xs:element name="customer" type="xs:token"/>
An element in your document might look like this:
<customer>John Smith</customer>
Or it might look like this:
<customer> John Smith </customer>
Note: In the example above the XML processor will remove the tabs.
Note that all of the data types below derives from the String data type (except from string itself)!
Name Description
ENTITIES
ENTITY
ID A string that represents the ID attribute in XML (only used with schema
attributes)
IDREF A string that represents the IDREF attribute in XML (only used with
schema attributes)
IDREFS
language A string that contains a valid language id
Name A string that contains a valid XML name
NCName
NMTOKEN A string that represents the NMTOKEN attribute in XML (only used with
schema attributes)
NMTOKENS
normalizedString A string that does not contain line feeds, carriage returns, or tabs
QName
string A string
token A string that does not contain line feeds, carriage returns, tabs, leading or
trailing spaces, or multiple spaces
• enumeration
• length
• maxLength
• minLength
• pattern (NMTOKENS, IDREFS, and ENTITIES can NOT use this constraint)
• whiteSpace
Date and time data types are used for values that contains date and time.
Time Zones
To specify a time zone, you can either enter a date in UTC time by adding a "Z" behind the date - like this:
<start>2002-09-24Z</start>
or you can specify an offset from the UTC time by adding a positive or negative time behind the date - like this:
<start>2002-09-24-06:00</start>
or
<start>2002-09-24+06:00</start>
Time Zones
To specify a time zone, you can either enter a time in UTC time by adding a "Z" behind the time - like this:
<start>09:30:10Z</start>
or you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this:
<start>09:30:10-06:00</start>
or
<start>09:30:10+06:00</start>
Time Zones
To specify a time zone, you can either enter a dateTime in UTC time by adding a "Z" behind the time - like
this:
<startdate>2002-05-30T09:30:10Z</startdate>
or you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this:
<startdate>2002-05-30T09:30:10-06:00</startdate>
or
<startdate>2002-05-30T09:30:10+06:00</startdate>
Negative Duration
Name Description
date Defines a date value
dateTime Defines a date and time value
duration Defines a time interval
gDay Defines a part of a date - the day (DD)
gMonth Defines a part of a date - the month (MM)
gMonthDay Defines a part of a date - the month and day (MM-DD)
gYear Defines a part of a date - the year (CCYY)
gYearMonth Defines a part of a date - the year and month (CCYY-MM)
time Defines a time value
Restrictions on Date Data Types
• enumeration
• maxExclusive
• maxInclusive
• minExclusive
• minInclusive
• pattern
• whiteSpace
The integer data type is used to specify a numeric value without a fractional component.
The following is an example of an integer declaration in a schema:
<xs:element name="prize" type="xs:integer"/>
An element in your document might look like this:
<prize>999</prize>
Or it might look like this:
<prize>+999</prize>
Or it might look like this:
<prize>-999</prize>
Or it might look like this:
<prize>0</prize>
Note that all of the data types below derives from the Decimal data type (except from decimal itself)!
Name Description
byte A signed 8-bit integer
decimal A decimal value
int A signed 32-bit integer
integer An integer value
long A signed 64-bit integer
negativeInteger An integer containing only negative values ( .., -2, -1.)
nonNegativeInteger An integer containing only non-negative values (0, 1, 2, ..)
nonPositiveInteger An integer containing only non-positive values (.., -2, -1, 0)
positiveInteger An integer containing only positive values (1, 2, ..)
short A signed 16-bit integer
unsignedLong An unsigned 64-bit integer
unsignedInt An unsigned 32-bit integer
unsignedShort An unsigned 16-bit integer
unsignedByte An unsigned 8-bit integer
• enumeration
• fractionDigits
• maxExclusive
• maxInclusive
• minExclusive
• minInclusive
• pattern
• totalDigits
• whiteSpace
Other miscellaneous data types are boolean, base64Binary, hexBinary, float, double,
anyURI, QName, and NOTATION.
Name Description
anyURI
base64Binary
boolean
double
float
hexBinary
NOTATION
QName
XSD Elements
Element Explanation
all Specifies that the child elements can appear in any order. Each child
element can occur 0 or 1 time
annotation Specifies the top-level element for schema comments
any Enables the author to extend the XML document with elements not
specified by the schema
anyAttribute Enables the author to extend the XML document with attributes not specified by
the schema
appInfo Specifies information to be used by the application (must go inside
annotation)
attribute Defines an attribute
attributeGroup Defines an attribute group to be used in complex type definitions
choice Allows only one of the elements contained in the <choice> declaration
to be present within the containing element
complexContent Defines extensions or restrictions on a complex type that contains
mixed content or elements only
complexType Defines a complex type element
documentation Defines text comments in a schema (must go inside annotation)
element Defines an element
extension Extends an existing simpleType or complexType element
field Specifies an XPath expression that specifies the value used to define an
identity constraint
group Defines a group of elements to be used in complex type definitions
import Adds multiple schemas with different target namespace to a document
include Adds multiple schemas with the same target namespace to a document
key Specifies an attribute or element value as a key (unique, non-nullable,
and always present) within the containing element in an instance
document
keyref Specifies that an attribute or element value correspond to those of the
specified key or unique element
list Defines a simple type element as a list of values
notation Describes the format of non-XML data within an XML document
redefine Redefines simple and complex types, groups, and attribute groups from
an external schema
restriction Defines restrictions on a simpleType, simpleContent, or a
complexContent
schema Defines the root element of a schema
selector Specifies an XPath expression that selects a set of elements for an
identity constraint
sequence Specifies that the child elements must appear in a sequence. Each child
element can occur from 0 to any number of times
simpleContent Contains extensions or restrictions on a text-only complex type or on a
simple type as content and contains no elements
simpleType Defines a simple type and specifies the constraints and information
about the values of attributes or text-only elements
union Defines a simple type as a collection (union) of values from specified
simple data types
unique Defines that an element or an attribute value must be unique within the
scope
The all element specifies that the child elements can appear in any order and that each child element can
occur zero or one time.
Element Information
Syntax
<all
id=ID
maxOccurs=1
minOccurs=0|1
any attributes
>
(annotation?,element*)
</all>
(The ? sign declares that the element can occur zero or one time, and the * sign declares that the element can
occur zero or more times inside the all element)
Attribute Description
id Optional. Specifies a unique ID for the element
maxOccurs Optional. Specifies the maximum number of times the element can occur.
The value must be 1.
minOccurs Optional. Specifies the minimum number of times the element can occur.
The value can be 0 or 1. Default value is 1
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
The example above indicates that the "firstname" and the "lastname" elements can appear in any order but
both elements MUST occur once and only once!
Example 2
<xs:element name="person">
<xs:complexType>
<xs:all minOccurs="0">
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
The example above indicates that the "firstname" and the "lastname" elements can appear in any order and
each element CAN appear zero or one time!
The annotation element is a top level element that specifies schema comments. The comments serve as inline
documentation.
Element Information
• Parent elements: Any element
Syntax
<annotation
id=ID
any attributes
>
(appinfo|documentation)*
</annotation>
(The * sign declares that the element can occur zero or more times inside the annotation element)
Attribute Description
id Optional. Specifies a unique ID for the element
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:appInfo>W3Schools Note</xs:appInfo>
<xs:documentation xml:lang="en">
This Schema defines a W3Schools note!
</xs:documentation>
</xs:annotation>
.
.
.
</xs:schema>
The any element enables the author to extend the XML document with elements not specified by the schema.
Element Information
Syntax
<any
id=ID
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
namespace=namespace
processContents=lax|skip|strict
any attributes
>
(annotation?)
</any>
(The ? sign declares that the element can occur zero or one time inside the any element)
Attribute Description
id Optional. Specifies a unique ID for the element
maxOccurs Optional. Specifies the maximum number of times the any element can occur
in the parent element. The value can be any number >= 0, or if you want to
set no limit on the maximum number, use the value "unbounded". Default
value is 1
minOccurs Optional. Specifies the minimum number of times the any element can occur
in the parent element. The value can be any number >= 0. Default value is 1
namespace Optional. Specifies the namespaces containing the elements that can be used.
Can be set to one of the following:
processContents Optional. Specifies how the XML processor should handle validation against
the elements specified by this any element. Can be set to one of the following:
• strict - the XML processor must obtain the schema for the required
namespaces and validate the elements (this is default)
• lax - same as strict but; if the schema cannot be obtained, no errors
will occur
• skip - The XML processor does not attempt to validate any elements
from the specified namespaces
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example shows a declaration for an element called "person". By using the <any> element the
author can extend (after <lastname>) the content of "person" with any element:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Element Information
Syntax
<anyAttribute
id=ID
namespace=namespace
processContents=lax|skip|strict
any attributes
>
(annotation?)
</anyAttribute>
(The ? sign declares that the element can occur zero or one time inside the anyAttribute element)
Attribute Description
id Optional. Specifies a unique ID for the element
namespace Optional. Specifies the namespaces containing the attributes that can be used.
Can be set to one of the following:
processContents Optional. Specifies how the XML processor should handle validation against
the elements specified by this any element. Can be set to one of the following:
• strict - the XML processor must obtain the schema for the required
namespaces and validate the elements (this is default)
• lax - same as strict but; if the schema cannot be obtained, no errors
will occur
• skip - The XML processor does not attempt to validate any elements
from the specified namespaces
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example shows a declaration for an element called "person". By using the <anyAttribute>
element the author can add any number of attributes to the "person" element:
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:element>
The appInfo element specifies information to be used by the application. This element must go within an
annotation element.
Element Information
Syntax
<appInfo
source=anyURL
>
Any well-formed XML content
</appInfo>
Attribute Description
source Optional. A URI reference that specifies the source of the application information
Example 1
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:appInfo>W3Schools Note</xs:appInfo>
<xs:documentation xml:lang="en">
This Schema defines a W3Schools note!
</xs:documentation>
</xs:annotation>
.
.
.
</xs:schema>
Element Information
• Parent elements: attributeGroup, schema, complexType, restriction (both
simpleContent and complexContent), extension (both simpleContent and
complexContent)
Syntax
<attribute
default=string
fixed=string
form=qualified|unqualified
id=ID
name=NCName
ref=QName
type=QName
use=optional|prohibited|required
any attributes
>
(annotation?,(simpleType?))
</attribute>
(The ? sign declares that the element can occur zero or one time inside the attribute element)
Attribute Description
default Optional. Specifies a default value for the attribute. Default and fixed
attributes cannot both be present
fixed Optional. Specifies a fixed value for the attribute. Default and fixed
attributes cannot both be present
form Optional. Specifies the form for the attribute. The default value is the
value of the attributeFormDefault attribute of the element containing
the attribute. Can be set to one of the following:
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
The example above indicates that the "code" attribute has a restriction. The only acceptable value is two of the
uppercase letters from a to z.
Example 2
To declare an attribute using an existing attribute definition within a complex type, use the ref attribute:
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="someComplexType">
<xs:attribute ref="code"/>
</xs:complexType>
Example 3
Attributes can have either a default value OR a fixed value specified. A default value is automatically assigned
to the attribute when no other value is specified. In the following example the default value is "EN":
<xs:attribute name="lang" type="xs:string" default="EN"/>
A fixed value is also automatically assigned to the attribute when no other value is specified. But unlike default
values; if you specify another value than the fixed, the document is considered invalid. In the following
example the fixed value is "EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/>
Example 4
All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute:
<xs:attribute name="lang" type="xs:string" use="optional"/>
To make an attribute required:
<xs:attribute name="lang" type="xs:string" use="required"/>
The attributeGroup element is used to group a set of attribute declarations so that they can be incorporated as
a group into complex type definitions.
Element Information
• Parent elements: attributeGroup, complexType, schema, restriction (both
simpleContent and complexContent), extension (both simpleContent and
complexContent)
Syntax
<attributeGroup
id=ID
name=NCName
ref=QName
any attributes
>
(annotation?),((attribute|attributeGroup)*,anyAttribute?))
</attributeGroup>
(The ? sign declares that the element can occur zero or one time, and the * sign declares that the element can
occur zero or more times inside the attributeGroup element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Optional. Specifies the name of the attribute group. Name and ref
attributes cannot both be present
ref Optional. Specifies a reference to a named attribute group. Name and
ref attributes cannot both be present
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
<xs:attributeGroup name="personattr">
<xs:attribute name="attr1" type="string"/>
<xs:attribute name="attr2" type="integer"/>
</xs:attributeGroup>
<xs:complexType name="person">
<xs:attributeGroup ref="personattr"/>
</xs:complexType>
The example above defines an attribute group named "personattr" which is used in a complex type named
"person".
The choice element allows only one of the elements contained in the <choice> declaration to be present within
the containing element.
Element Information
Syntax
<choice
id=ID
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
any attributes
>
(annotation?,(element|group|choice|sequence|any)*)
</choice>
(The ? sign declares that the element can occur zero or one time, and the * sign declares that the element can
occur zero or more times inside the choice element)
Attribute Description
id Optional. Specifies a unique ID for the element
maxOccurs Optional. Specifies the maximum number of times the choice element
can occur in the parent element. The value can be any number >= 0,
or if you want to set no limit on the maximum number, use the value
"unbounded". Default value is 1
minOccurs Optional. Specifies the minimum number of times the choice element
can occur in the parent the element. The value can be any number >=
0. Default value is 1
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>
</xs:choice>
</xs:complexType>
</xs:element>
The example above defines an element named "person" which must contain either a "employee" element or a
"member" element.
The complexContent element defines extensions or restrictions on a complex type that contains mixed content
or elements only.
Element Information
Syntax
<complexContent
id=ID
mixed=true|false
any attributes
>
(annotation?,(restriction|extension))
</complexContent>
(The ? sign declares that the element can occur zero or one time inside the complexContent element)
Attribute Description
id Optional. Specifies a unique ID for the element
mixed Optional. Specifies whether character data is allowed to appear
between the child elements of this complexType element. Default is
false
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example has a complex type, "fullpersoninfo", that derives from another complex type,
"personinfo", by extending the inherited type with three additional elements (address, city and country):
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
In the example above the "employee" element must contain, in sequence, the following elements: "firstname",
"lastname", "address", "city", and "country".
The complexType element defines a complex type. A complex type element is an XML element that contains
other elements and/or attributes.
Element Information
Syntax
<complexType
id=ID
name=NCName
abstract=true|false
mixed=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>
(annotation?,(simpleContent|complexContent|((group|all|
choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))))
</complexType>
(The ? sign declares that the element can occur zero or one time, and the * sign declares that the element can
occur zero or more times inside the complexType element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Optional. Specifies a name for the element
abstract Optional. Specifies whether the complex type can be used in an
instance document. True indicates that an element cannot use this
complex type directly but must use a complex type derived from this
complex type. Default is false
mixed Optional. Specifies whether character data is allowed to appear
between the child elements of this complexType element. Default is
false. If a simpleContent element is a child element, the mixed attribute
is not allowed!
block Optional. Prevents a complex type that has a specified type of
derivation from being used in place of this complex type. This value can
contain #all or a list that is a subset of extension or restriction:
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example has an element named "note" that is of a complex type:
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Example 2
The following example has a complex type, "fullpersoninfo", that derives from another complex type,
"personinfo", by extending the inherited type with three additional elements (address, city and country):
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
In the example above the "employee" element must contain, in sequence, the following elements: "firstname",
"lastname", "address", "city", and "country".
The documentation element is used to enter text comments in a schema. This element must go inside an
annotation element.
Element Information
Syntax
<documentation
source=URI reference
xml:lang=language
>
Any well-formed XML content
</documentation>
Attribute Description
source Optional. Specifies the source of the application information
xml:lang Optional. Specifies the language used in the contents
Example 1
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:appInfo>W3Schools Note</xs:appInfo>
<xs:documentation xml:lang="en">
This Schema defines a W3Schools note!
</xs:documentation>
</xs:annotation>
.
.
.
</xs:schema>
Element Information
Syntax
<element
id=ID
name=NCName
ref=QName
type=QName
substitutionGroup=QName
default=string
fixed=string
form=qualified|unqualified
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
nullable=true|false
abstract=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>
annotation?,((simpleType|complexType)?,(unique|key|keyref)*))
</element>
(The ? sign declares that the element can occur zero or one time, and the * sign declares that the element can
occur zero or more times inside the element element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Optional. Specifies a name for the element. This attribute is required if
the parent element is the schema element
ref Optional. Refers to the name of another element. The ref attribute can
include a namespace prefix. This attribute cannot be used if the parent
element is the schema element
type Optional. Specifies either the name of a built-in data type, or the name
of a simpleType or complexType element
substitutionGroup Optional. Specifies the name of an element that can be substituted with
this element. This attribute cannot be used if the parent element is not
the schema element
default Optional. Specifies a default value for the element (can only be used if
the element's content is a simple type or text only)
fixed Optional. Specifies a fixed value for the element (can only be used if
the element's content is a simple type or text only)
form Optional. Specifies the form for the element. "unqualified" indicates that
this attribute is not required to be qualified with the namespace prefix.
"qualified" indicates that this attribute must be qualified with the
namespace prefix. The default value is the value of the
elementFormDefault attribute of the schema element. This attribute
cannot be used if the parent element is the schema element
maxOccurs Optional. Specifies the maximum number of times this element can
occur in the parent element. The value can be any number >= 0, or if
you want to set no limit on the maximum number, use the value
"unbounded". Default value is 1. This attribute cannot be used if the
parent element is the schema element
minOccurs Optional. Specifies the minimum number of times this element can
occur in the parent element. The value can be any number >= 0.
Default value is 1. This attribute cannot be used if the parent element is
the schema element
nullable Optional. Specifies whether an explicit null value can be assigned to the
element. True enables an instance of the element to have the null
attribute set to true. The null attribute is defined as part of the XML
Schema namespace for instances. Default is false
abstract Optional. Specifies whether the element can be used in an instance
document. True indicates that the element cannot appear in the
instance document. Instead, another element whose substitutionGroup
attribute contains the qualified name (QName) of this element must
appear in this element's place. Default is false
block Optional. Prevents an element with a specified type of derivation from
being used in place of this element. This value can contain #all or a list
that is a subset of extension, restriction, or equivClass:
final Optional. Sets the default value of the final attribute on the element
element. This attribute cannot be used if the parent element is not the
schema element. This value can contain #all or a list that is a subset of
extension or restriction:
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example is a schema with four simple elements named "fname", "lname", "age", and "dateborn",
which are of type string, nonNegativeInteger, and date:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="fname" type="xs:string"/>
<xs:element name="lname" type="xs:string"/>
<xs:element name="age" type="xs:nonNegativeInteger"/>
<xs:element name="dateborn" type="xs:date"/>
</xs:schema>
Example 2
The following example is a schema with an element named "note" that is of a complex type. The "note"
element contains four other simple elements; "to", "from", "heading", and "body":
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Example 3
This example is equal to Example 2, but here we have chosen to use the ref attribute to refer to the element
names:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element ref="to"/>
<xs:element ref="from"/>
<xs:element ref="heading"/>
<xs:element ref="body"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:schema>
Element Information
• Parent elements: simpleContent, complexContent
Syntax
<extension
id=ID
base=QName
any attributes
>
(annotation?,((group|all|choice|sequence)?,
((attribute|attributeGroup)*,anyAttribute?)))
</extension>
(The ? sign declares that the element can occur zero or one time, and the * sign declares that the element can
occur zero or more times inside the extension element)
Attribute Description
id Optional. Specifies a unique ID for the element
base Optional. Specifies the name of a built-in data type, a simpleType
element, or a complexType element
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
Example 2
The following example extends an existing complexType element by adding three elements:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
The field element specifies an XPath expression that specifies the value used to define an identity constraint.
Element Information
Syntax
<field
id=ID
xpath=XPath expression
any attributes
>
(annotation?)
</field>
(The ? sign declares that the element can occur zero or one time inside the field element)
Attribute Description
id Optional. Specifies a unique ID for the element
xpath Required. Identifies a single element or attribute whose content or
value is used for the constraint
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example shows a field element that specifies the "userID" attribute as the field to use for the
identity constraint:
<xs:field xpath="@userID"/>
The group element is used to define a group of elements to be used in complex type definitions.
Element Information
Syntax
<group
id=ID
name=NCName
ref=QName
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
any attributes
>
(annotation?,(all|choice|sequence))
</group>
(The ? sign declares that the element can occur zero or one time inside the group element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Optional. Specifies a name for the group. This attribute is used only
when the schema element is the parent of this group element. Name
and ref attributes cannot both be present
ref Optional. Refers to the name of another group. Name and ref attributes
cannot both be present
maxOccurs Optional. Specifies the maximum number of times the group element
can occur in the parent element. The value can be any number >= 0,
or if you want to set no limit on the maximum number, use the value
"unbounded". Default value is 1
minOccurs Optional. Specifies the minimum number of times the group element
can occur in the parent element. The value can be any number >= 0.
Default value is 1
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example defines a group containing a sequence of four elements and uses the group element in
a complex type definition:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="custGroup">
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="orderdetails" type="xs:string"/>
<xs:element name="billto" type="xs:string"/>
<xs:element name="shipto" type="xs:string"/>
</xs:sequence>
</xs:group>
The import element is used to add multiple schemas with different target namespace to a document.
Element Information
Syntax
<import
id=ID
namespace=anyURI
schemaLocation=anyURI
any attributes
>
(annotation?)
</import>
(The ? sign declares that the element can occur zero or one time inside the import element)
Attribute Description
id Optional. Specifies a unique ID for the element
namespace Required. Specifies the URI of the namespace to import
schemaLocation Optional. Specifies the URI to the schema for the imported namespace
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
Element Information
Syntax
<include
id=ID
schemaLocation=anyURI
any attributes
>
(annotation?)
</include>
(The ? sign declares that the element can occur zero or one time inside the include element)
Attribute Description
id Optional. Specifies a unique ID for the element
schemaLocation Required. Specifies the URI to the schema to include in the target
namespace of the containing schema
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
With included schemas, the included files must all reference the same target namespace. If the schema target
namespace don't match, the include won't work:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com/schema">
<xs:include
schemaLocation="http://www.w3schools.com/schema/customer.xsd"/>
<xs:include
schemaLocation="http://www.w3schools.com/schema/company.xsd"/>
..
..
..
</xs:schema>
The key element specifies an attribute or element value as a key (unique, non-nullable, and always present)
within the containing element in an instance document.
The key element MUST contain the following (in order):
• one and only one selector element (contains an XPath expression that specifies the set
of elements across which the values specified by field must be unique)
• one or more field elements (contains an XPath expression that specifies the values that
must be unique for the set of elements specified by the selector element)
Element Information
Syntax
<key
id=ID
name=NCName
any attributes
>
(annotation?,(selector,field+))
</key>
(The ? sign declares that the element can occur zero or one time, and the + sign declares that the element
must occur one or more times inside the key element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Required. Specifies the name of the key element
any attributes Optional. Specifies any other attributes with non-schema namespace
The keyref element specifies that an attribute or element value correspond to those of the specified key or
unique element.
The keyref element MUST contain the following (in order):
• one and only one selector element (contains an XPath expression that specifies the set
of elements across which the values specified by field must be unique)
• one or more field elements (contains an XPath expression that specifies the values that
must be unique for the set of elements specified by the selector element)
Element Information
Syntax
<key
id=ID
name=NCName
refer=QName
any attributes
>
(annotation?,(selector,field+))
</include>
(The ? sign declares that the element can occur zero or one time, and the + sign declares that the element
must occur one or more times inside the keyref element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Required. Specifies the name of the keyref element
refer Required. Specifies the name of a key or unique element defined in this
or another schema
any attributes Optional. Specifies any other attributes with non-schema namespace
The list element defines a simple type element as a list of values of a specified data type.
Element Information
Syntax
<list
id=ID
itemType=QName
any attributes
>
(annotation?,(simpleType?))
</list>
(The ? sign declares that the element can occur zero or one time inside the list element)
Attribute Description
id Optional. Specifies a unique ID for the element
itemType Specifies the name of a built-in data type or simpleType element defined in this or
another schema. This attribute is not allowed if the content contains a simpleType
element, otherwise it is required
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
Example 2
The following example shows a simple type that is a list of strings:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="stringvalues" type="valuelist">
<xs:simpleType name="valuelist">
<xs:list itemType="xs:string"/>
</xs:simpleType>
</xs:schema>
The "stringvalues" element in a document could look like this (notice that the list will have four list items):
<stringvalues>I love XML Schema</stringvalues>
The notation element describes the format of non-XML data within an XML document.
Element Information
Syntax
<notation
id=ID
name=NCName
public=anyURI
system=anyURI
any attributes
>
(annotation?)
</notation>
(The ? sign declares that the element can occur zero or one time inside the notation element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Required. Specifies a name for the element
public Required. Specifies a URI corresponding to the public identifier
system Optional. Specifies a URI corresponding to the system identifier
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example shows a gif and a jpeg notation using a viewer application, view.exe:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:notation name="gif" public="image/gif" system="view.exe"/>
<xs:notation name="jpeg" public="image/jpeg" system="view.exe"/>
<xs:element name="image">
<xs:complexType>
<xs:simpleContent>
<xs:attribute name="type">
<xs:simpleType>
<xs:restriction base="xs:NOTATION">
<xs:enumeration value="gif"/>
<xs:enumeration value="jpeg"/>
<xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
The "image" element in a document could look like this:
<image type="gif"></image>
The redefine element redefines simple and complex types, groups, and attribute groups from an external
schema.
Element Information
Syntax
<redefine
id=ID
schemaLocation=anyURI
any attributes
>
(annotation|(simpleType|complexType|group|attributeGroup))*
</redefine>
Attribute Description
id Optional. Specifies a unique ID for the element
schemaLocation Required. A URI to the location of a schema document
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The following example shows a schema, Myschama2.xsd, with elements specified by the Myschama1.xsd.
The pname type is redefined. According to this schema, elements constrained by the pname type must end
with a "country" element:
Myschema1.xsd:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="pname">
<xs:sequence>
<xs:element name="firstname"/>
<xs:element name="lastname"/>
</xs:sequence>
</xs:complexType>
Element Information
Syntax
<restriction
id=ID
base=QName
any attributes
>
Content for simpleType:
(annotation?,(simpleType?,(minExclusive|minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*))
Content for simpleContent:
(annotation?,(simpleType?,(minExclusive |minInclusive|
maxExclusive|maxInclusive|totalDigits|fractionDigits|
length|minLength|maxLength|enumeration|whiteSpace|pattern)*)?,
((attribute|attributeGroup)*,anyAttribute?))
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
This example defines an element called "age" with a restriction. The value of age can NOT be lower than 0 or
greater than 100:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 2
This example also defines an element called "initials". The "initials" element is a simple type with a restriction.
The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 3
This example defines an element called "password". The "password" element is a simple type with a
restriction. The value must be minimum five characters and maximum eight characters:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Example 4
This example shows a complex type definition using restriction. The complex type "Norwegian_customer" is
derived from a general customer complex type and its country element is fixed to "Norway":
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Norwegian_customer">
<xs:complexContent>
<xs:restriction base="customer">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="country" type="xs:string" fixed="Norway"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
Element Information
Syntax
<schema
id=ID
attributeFormDefault=qualified|unqualified
elementFormDefault=qualified|unqualified
blockDefault=(#all|list of (extension|restriction|substitution))
finalDefault=(#all|list of (extension|restriction|list|union))
targetNamespace=anyURI
version=token
xmlns=anyURI
any attributes
>
((include|import|redefine|annotation)*,(((simpleType|complexType|
group|attributeGroup)|element|attribute|notation),annotation*)*)
</schema>
Attribute Description
id Optional. Specifies a unique ID for the element
attributeFormDefault Optional. The form for attributes declared in the target namespace of
this schema. The value must be "qualified" or "unqualified". Default is
"unqualified". "unqualified" indicates that attributes from the target
namespace are not required to be qualified with the namespace prefix.
"qualified" indicates that attributes from the target namespace must be
qualified with the namespace prefix
elementFormDefault Optional. The form for elements declared in the target namespace of
this schema. The value must be "qualified" or "unqualified". Default is
"unqualified". "unqualified" indicates that elements from the target
namespace are not required to be qualified with the namespace prefix.
"qualified" indicates that elements from the target namespace must be
qualified with the namespace prefix
blockDefault Optional. Specifies the default value of the block attribute on element
and complexType elements in the target namespace. The block
attribute prevents a complex type (or element) that has a specified
type of derivation from being used in place of this complex type. This
value can contain #all or a list that is a subset of extension, restriction,
or substitution:
finalDefault Optional. Specifies the default value of the final attribute on element,
simpleType, and complexType elements in the target namespace. The
final attribute prevents a specified type of derivation of an element,
simpleType, or complexType element. For element and complexType
elements, this value can contain #all or a list that is a subset of
extension or restriction. For simpleType elements, this value can
additionally contain list and union:
Example 1
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="values" type="xs:string">
</xs:schema>
Example 2
In this example, the schema components (element name, type) in the http://www.w3.org/2001/XMLSchema
namespace are unqualified and those for http://www.w3schools.com/w3schoolsschema (mystring) are
qualified with the wsc prefix:
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:wsc="http://www.w3schools.com/w3shoolsschema">
<element name="fname" type="wsc:mystring"/>
</schema>
The selector element specifies an XPath expression that selects a set of elements for an identity constraint
(unique, key, and keyref elements).
Element Information
Syntax
<selector
id=ID
xpath=a subset of XPath expression
any attributes
>
(annotation?)
</selector>
(The ? sign declares that the element can occur zero or one time inside the selector element)
Attribute Description
id Optional. Specifies a unique ID for the element
xpath Required. Specifies an XPath expression, relative to the element being
declared, that identifies the child elements to which the identity
constraint applies
any attributes Optional. Specifies any other attributes with non-schema namespace
The sequence element specifies that the child elements must appear in a sequence. Each child element can
occur from 0 to any number of times.
Element Information
Syntax
<sequence
id=ID
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
any attributes
>
(annotation?,(element|group|choice|sequence|any)*)
</sequence>
(The ? sign declares that the element can occur zero or one time inside the sequence element)
Attribute Description
id Optional. Specifies a unique ID for the element
maxOccurs Optional. Specifies the maximum number of times the sequence element can
occur in the parent element. The value can be any number >= 0, or if you want
to set no limit on the maximum number, use the value "unbounded". Default
value is 1
minOccurs Optional. Specifies the minimum number of times the sequence element can
occur in the parent element. The value can be any number >= 0. Default value
is 1
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
This example shows a declaration for an element called "personinfo", which must contain the following five
elements in order; "firstname", "lastname", "address", "city", and "country":
<xs:element name="personinfo">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Example 2
This example shows a declaration for an element called "pets" that can have zero or more of the following
elements, dog and cat, in the sequence element:
<xs:element name="pets">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="dog" type="xs:string"/>
<xs:element name="cat" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The simpleContent element contains extensions or restrictions on a text-only complex type or on a simple type
as content and contains no elements.
Element Information
<simpleContent
id=ID
any attributes
>
(annotation?,(restriction|extension))
</simpleContent>
(The ? sign declares that the element can occur zero or one time inside the simpleContent element)
Attribute Description
id Optional. Specifies a unique ID for the element
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
The simpleType element defines a simple type and specifies the constraints and information about the values
of attributes or text-only elements.
Element Information
Syntax
<simpleType
id=ID
name=NCName
any attributes
>
(annotation?,(restriction|list|union))
</simpleType>
(The ? sign declares that the element can occur zero or one time inside the simpleType element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Specifies a name for the element. This attribute is required if the simpleType
element is a child of the schema element, otherwise it is not allowed
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
This example defines an element called "age" that is a simple type with a restriction. The value of age can
NOT be lower than 0 or greater than 100:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The union element defines a simple type as a collection (union) of values from specified simple data types.
Element Information
Syntax
<union
id=ID
memberTypes="list of QNames"
any attributes
>
(annotation?,(simpleType*))
</union>
(The ? sign declares that the element can occur zero or one time inside the union element)
Attribute Description
id Optional. Specifies a unique ID for the element
memberTypes Optional. Specifies a list of built-in data types or simpleType elements defined in
a schema
any attributes Optional. Specifies any other attributes with non-schema namespace
Example 1
This example shows a simple type that is a union of two simple types:
<xsd:element name="jeans_size">
<xsd:simpleType>
<xsd:union memberTypes="sizebyno sizebystring" />
</xsd:simpleType>
</xsd:element>
<xsd:simpleType name="sizebyno">
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxInclusive="42"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sizebystring">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="small"/>
<xsd:enumeration value="medium"/>
<xsd:enumeration value="large"/>
</xsd:restriction>
</xsd:simpleType>
The unique element defines that an element or an attribute value must be unique within the scope.
The unique element MUST contain the following (in order):
• one and only one selector element (contains an XPath expression that specifies the set
of elements across which the values specified by field must be unique)
• one or more field elements (contains an XPath expression that specifies the values that
must be unique for the set of elements specified by the selector element)
Element Information
Syntax
<unique
id=ID
name=NCName
any attributes
>
(annotation?,(selector,field+))
</unique>
(The ? sign declares that the element can occur zero or one time inside the unique element)
Attribute Description
id Optional. Specifies a unique ID for the element
name Required. Specifies a name for the element
any attributes Optional. Specifies any other attributes with non-schema namespace