2. What is an XML Schema?
• An XML Schema describes the structure of an XML
document.
• The XML Schema language is also referred to as
XML Schema Definition (XSD).
• XML documents can have a reference to a DTD or
to an XML Schema.
• The purpose of a Schema is to define the legal
building blocks of an XML document, just like a
DTD.
3. An XML Schema:
• defines elements that can appear in a document
• defines attributes that can appear within elements
• defines which elements are child elements
• defines the sequence in which the child elements can
appear
• defines the number of child elements
• defines whether an element is empty or can include text
• defines default values for attributes
4. Schema vs. DTD
• XML Schemas are extensible to future
additions
• XML Schemas are richer and more powerful
than DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces
5. Purpose of an XML Schema
• The purpose of an XML Schema is to define
the legal building blocks of an XML
document:
– the elements and attributes that can appear in a
document
– the number of (and order of) child elements
– data types for elements and attributes
– default and fixed values for elements and attributes
6. XSD - The <schema> Element
• The <schema> element is the root element of
every XML Schema.
• <?xml version="1.0"?>
<xs:schema>
...
...
</xs:schema>
• The <schema> element may contain some
attributes.
7. Referring to a schema
• To refer to a DTD in an XML document, the reference goes before the root
element:
– <?xml version="1.0"?>
<!DOCTYPE rootElement SYSTEM "url">
<rootElement> ... </rootElement>
• To refer to an XML Schema in an XML document, the reference goes in the
root element:
– <?xml version="1.0"?>
<rootElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
(The XML Schema Instance reference is required)
xsi:noNamespaceSchemaLocation="url.xsd">
(This is where your XML Schema definition can be found)
...
</rootElement>
7
8. “Simple” and “complex” elements
• A “simple” element is one that contains text and
nothing else
– A simple element cannot have attributes
– A simple element cannot contain other elements
– A simple element cannot be empty
– However, the text can be of many different types,
and may have various restrictions applied to it
• If an element isn’t simple, it’s “complex”
– A complex element may have attributes
– A complex element may be empty, or it may contain
text, other elements, or both text and other elements
9. Defining a simple element
• A simple element is defined as
<xs:element name="name" type="type" />
where:
– name is the name of the element
– the most common values for type are
xs:boolean xs:integer
xs:date xs:string
xs:decimal xs:time
• Other attributes a simple element may have:
– default="default value" if no other value is
specified
– fixed="value" no other value may be
specified
10. Example of some XML elements:
• <lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-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"/>
12. Default and Fixed Values for Simple Elements
• Simple elements may have a default value OR a fixed
value specified.
• Default value: Automatically assigned to the element
when no other value is specified.
– In the following example the default value is "red":
– <xs:element name="color" type="xs:string"
default="red"/>
• Fixed value : Automatically assigned to the element,
and you cannot specify another value.
– In the following example the fixed value is "red":
– <xs:element name="color" type="xs:string" fixed="red"/>
13. Defining an attribute
• Attributes themselves are always declared as
simple types
• An attribute is defined as
<xs:attribute name="name" type="type" />
where:
– name and type are the same as for xs:element
• Other attributes a simple element may have:
– default="default value" if no other value is specified
– fixed="value" no other value may be specified
– use="optional" the attribute is not required
(default)
– use="required" the attribute must be present
14. XML Schemas Secure Data Communication
• When sending data from a sender to a receiver, it is
essential that both parts have the same "expectations" about
the content.
• With XML Schemas, the sender can describe the data in a
way that the receiver will understand.
• A date like: "03-11-2004" will, in some countries, be
interpreted as 3.November and in other countries as
11.March.
• However, an XML element with a data type like this:
• <date type="date">2004-03-11</date>
• ensures a mutual understanding of the content, because the
XML data type "date" requires the format "YYYY-MM-
DD".
15. XSD Restrictions
• Restrictions on Values
• Restrictions on a Set of Values
• Restrictions on a Series of Values
• Restrictions on Whitespace Characters
• Restrictions on Length
16. Restrictions on Values
• The general form for putting a restriction on a text value is:
– <xs:element name="name"> (or xs:attribute)
<xs:restriction base="type">
... the restrictions ...
</xs:restriction>
</xs:element>
• For example:
– <xs:element name="age">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0">
<xs:maxInclusive value="140">
</xs:restriction>
</xs:element>
17. Restrictions on numbers
• minInclusive -- number must be ≥ the given value
• minExclusive -- number must be > the given value
• maxInclusive -- number must be ≤ the given value
• maxExclusive -- number must be < the given value
• totalDigits -- number must have exactly value digits
• fractionDigits -- number must have no more than value
digits after the decimal point
18. Restrictions on a Set of Values
• An enumeration restricts the value to be one of a fixed set of
values
• The example below defines an element called "car" with a
restriction. The only acceptable values are: Audi, Golf, BMW:
• <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>
19. Restrictions on a Series of Values
• 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.
• The example below defines an element called "letter" with a
restriction. The only acceptable value is ONE of the
LOWERCASE letters from a to z:
• <xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
20. Restrictions on a Series of Values
(continued)
• The next example defines an element called "initials"
with a restriction. The only acceptable value is
THREE of the UPPERCASE letters from a to z:
• <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>
21. Restrictions on a Series of Values
(continued)
• The next example also defines an element called
"initials" 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>
22. Restrictions on a Series of Values
(continued)
• The next example defines an element called
"choice" with a restriction. The only acceptable
value is ONE of the following letters: x, y, OR z:
• <xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
23. Restrictions on a Series of Values
(continued)
• The next example defines an element called "prodid"
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:
• <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>
24. Restrictions on Whitespace
Characters
• whiteSpace -- not really a
“restriction”--tells what to
do with whitespace
– value="preserve" Keep
all whitespace
– value="replace"
Change all whitespace
characters to spaces
– value="collapse"
Remove leading and trailing
whitespace, and
replace all
sequences of whitespace with
a single space
<xs:element
name="address">
<xs:simpleType>
<xs:restriction
base="xs:string">
<xs:whiteSpace
value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
25. 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" with a
restriction. The value must be exactly eight characters:
• <xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
26. Restrictions on Length (continued)
• This example defines another element called "password" 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>
27. Predefined date and time types
• xs:date -- A date in the format CCYY-MM-DD,
for example, 2002-11-05
• xs:time -- A date in the format hh:mm:ss (hours,
minutes, seconds)
• xs:dateTime -- Format is CCYY-MM-
DDThh:mm:ss
– The T is part of the syntax
• Allowable restrictions on dates and times:
– enumeration, minInclusive, minExclusive,
maxInclusive, maxExclusive, pattern, whiteSpace
28. Predefined numeric types
• Here are some of the predefined numeric types:
• Allowable restrictions on numeric types:
– enumeration, minInclusive, minExclusive, maxInclusive, maxExclusive,
fractionDigits, totalDigits, pattern, whiteSpace
xs:decimal xs:positiveInteger
xs:byte xs:negativeInteger
xs:short xs:nonPositiveInteger
xs:int xs:nonNegativeInteger
xs:long