You are on page 1of 7

What is XML Schema?

XML Schema is an XML based alternative to DTD


An XML schema describes the structure of an XML document
The XML Schema language is also referred to as XML Schema
XML Schema Definition (XSD)

The purpose of an XML Schema is to define the legal building blocks of


:: based on W3Schools tutorial :: an XML document, just like a DTD

Institute of Computing Science defines elements that can appear in a document


Poznań University of Technology
defines attributes that can appear in a document
defines which elements are child elements
v1.9, 2008/03/04
defines the order of child elements
defines the number of child elements (NEW!)
defines whether an element is empty or can include text
defines data types for elements and attributes (NEW!)
defines default and fixed values for elements and attributes

:: based on W3Schools tutorial :: XML Schema [1/25]

Why not DTD? XML Schema Advantages


XML Schemas are extensible to future additions
XML Schemas are richer and more useful than DTDs
XML Schemas are written in XML
Secure Data Communication
You don’t have to learn another language
You can use your XML editor to edit your Schema files data type date requires the format CCYY-MM-DD
You can use your XML parser to parse your Schema files
<date type="date">1999-03-11</date>
You can manipulate your Schema with the XML DOM
You can transform your Schema with XSLT
XML Schemas are Extensible
XML Schemas support data types
It is easier to describe permissible document content Reuse your schema in other schemas
It is easier to validate the correctness of data Create your own data types derived from standard types
It is easier to work with data from a database Reference multiple schemas from the same document
It is easier to define data facets (restrictions on data)
It is easier to define data patterns (data formats)
It is easier to convert data between different data types
XML Schemas support namespaces
Well-Formed is not enough
:: based on W3Schools tutorial :: XML Schema [2/25] :: based on W3Schools tutorial :: XML Schema [3/25]
Example XML Document with DTD Example XML Schema

XML document <?xml version="1.0"?>

<xs:schema
<?xml version="1.0"?>
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.put.poznan.pl"
<note>
xmlns="http://www.put.poznan.pl"
<to>Tove</to>
elementFormDefault="qualified">
<from>Jani</from>
<heading>Reminder</heading>
<xs:element name="note">
<body>Don’t forget me this weekend!</body>
<xs:complexType>
</note>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
DTD <xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<!ELEMENT note (to, from, heading, body)> <xs:element name="body" type="xs:string"/>
<!ELEMENT to (#PCDATA)> </xs:sequence>
<!ELEMENT from (#PCDATA)> </xs:complexType>
<!ELEMENT heading (#PCDATA)> </xs:element>
<!ELEMENT body (#PCDATA)>
</xs:schema>

:: based on W3Schools tutorial :: XML Schema [4/25] :: based on W3Schools tutorial :: XML Schema [5/25]

Reference to Formal Document Description The <schema> Element

<xs:schema

Reference to DTD
XS namespace:
<!DOCTYPE note SYSTEM "http://www.put.poznan.pl/dtd/note.dtd">
xmlns:xs="http://www.w3.org/2001/XMLSchema"

Reference to XML Schema


Namespace of the document:
<note xmlns="http://www.put.poznan.pl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="http://www.put.poznan.pl"
xsi:schemaLocation="http://www.put.poznan.pl/note.xsd">
Default namespace:
<to>Tove</to>
<from>Jani</from> xmlns="http://www.put.poznan.pl"
<heading>Reminder</heading>
<body>Don’t forget me this weekend!</body>
</note> Any elements used by the XML instance document which were declared
in this schema must be namespace qualified:

elementFormDefault="qualified">

:: based on W3Schools tutorial :: XML Schema [6/25] :: based on W3Schools tutorial :: XML Schema [7/25]
Types Attributes

Complex Type contains other elements


Simple Type can contain only text. It cannot contain any other
elements or attributes
All attributes are declared as simple types
Simple elements Only complex elements can have attributes

<xs:element name="xxx" type="yyy"/> Definition

Common data types: <xs:attribute name="xxx" type="yyy"/>

xs:string xs:decimal xs:integer All attributes are optional by default


xs:boolean xs:date xs:time
<xs:attribute name="lang" type="xs:string" use="optional"/>
Default value: <xs:attribute name="lang" type="xs:string" use="required"/>

<xs:element name="color" type="xs:string" default="red"/>


<xs:element name="color" type="xs:string" fixed="red"/>
^^^^^
cannot be modified

:: based on W3Schools tutorial :: XML Schema [8/25] :: based on W3Schools tutorial :: XML Schema [9/25]

XML Schema Restrictions Enumerated Restrictions

<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
Restrictions on XML elements are called facets <xs:enumeration value="Audi"/>
<xs:enumeration value="Honda"/>
Example: age must be between 0 and 100 <xs:enumeration value="BMW"/>
</xs:restriction>
<xs:element name="age"> </xs:simpleType>
</xs:element>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
or define a new type carType:
<xs:maxInclusive value="100"/>
</xs:restriction> <xs:element name="car" type="carType"/>
</xs:simpleType>
<xs:simpleType name="carType">
</xs:element> <xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Honda"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>

:: based on W3Schools tutorial :: XML Schema [10/25] :: based on W3Schools tutorial :: XML Schema [11/25]
Pattern Restrictions White Spaces

Example

Example <xs:element name="address">

<xs:element name="letter"> <xs:simpleType>


<xs:restriction base="xs:string">
<xs:simpleType> <xs:whiteSpace value="preserve"/>
<xs:restriction base="xs:string"> </xs:restriction>
<xs:pattern value="[a-z]"/> </xs:simpleType>
</xs:restriction>
</xs:simpleType> </xs:element>

</xs:element> Possible values

Regular expressions, e.g.: zipcode preserve white spaces will be preserved from the document
replace all line feeds, tabs, spaces, and carriage returns will be
<xs:pattern value="[0-9]{2}-[0-9]{3}"/>
replaced with spaces
collapse all white space characters will be replaced with a single
space

:: based on W3Schools tutorial :: XML Schema [12/25] :: based on W3Schools tutorial :: XML Schema [13/25]

Restrictions on Lengths Complex Types


A complex element contains other elements and/or attributes.
Example Types of complex types
<xs:element name="password">
<xs:simpleType> empty elements
<xs:restriction base="xs:string"> elements that contain only other elements
<xs:length value="8"/>
</xs:restriction> elements that contain only text
</xs:simpleType> elements that contain both other elements and text
</xs:element>

Examples
Length range
1. <product pid="1345"/>
<xs:simpleType> 2. <employee>
<xs:restriction base="xs:string"> <firstname>John</firstname>
<xs:minLength value="5"/> <lastname>Smith</lastname>
<xs:maxLength value="8"/> </employee>
</xs:restriction> 3. <food type="dessert">Ice cream</food>
</xs:simpleType> 4. <description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>

:: based on W3Schools tutorial :: XML Schema [14/25] :: based on W3Schools tutorial :: XML Schema [15/25]
Complex Type Definition Type Inheritance
Examples

<xs:element name="employee"> <xs:complexType name="personinfo">


<xs:complexType> <xs:sequence>
<xs:sequence> <xs:element name="firstname" type="xs:string"/>
<xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/> </xs:sequence>
</xs:sequence> </xs:complexType>
</xs:complexType>
</xs:element> <xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
Elements in sequence must come in that order. <xs:sequence>
<xs:element name="address" type="xs:string"/>
Named types
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="employee" type="personinfo"/> </xs:sequence>
<xs:complexType name="personinfo"> </xs:extension>
<xs:sequence> </xs:complexContent>
<xs:element name="firstname" type="xs:string"/> </xs:complexType>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>

:: based on W3Schools tutorial :: XML Schema [16/25] :: based on W3Schools tutorial :: XML Schema [17/25]

Complex Type Definition Examples Indicators


Single attribute
Indicators specify HOW elements are to be used in documents.
<xs:element name="product"> Order indicators:
<xs:complexType>
<xs:attribute name="prodID" type="xs:positiveInteger"/>
</xs:complexType> All — all child elements must occur in any order
</xs:element> Choice — either one child element or another
Sequence — child elements must appear in a specific order
Text only

<xs:element name="somename"> Occurrence indicators:


<xs:complexType>
<xs:simpleContent> maxOccurs — number or unbounded
<xs:restriction base="basetype">
... minOccurs
</xs:restriction>
</xs:simpleContent> Group indicators:
</xs:complexType>
</xs:element>
<group> — defines an element group
<xs:complexType mixed="true"> allows to mix subelemements and <attributeGroup> — defines an attribute group
text
:: based on W3Schools tutorial :: XML Schema [18/25] :: based on W3Schools tutorial :: XML Schema [19/25]
Indicator Example (I) Indicator Example (II)
<xs:element name="persons">
<xs:complexType> <xs:group name="persongroup">
<xs:sequence> <xs:sequence>
<xs:element name="person" maxOccurs="unbounded"> <xs:element name="firstname" type="xs:string"/>
<xs:complexType> <xs:element name="lastname" type="xs:string"/>
<xs:sequence> <xs:element name="birthday" type="xs:date"/>
<xs:element name="full_name" type="xs:string"/> </xs:sequence>
<xs:element name="child_name" type="xs:string" </xs:group>
minOccurs="0" maxOccurs="5"/>
</xs:sequence> <xs:complexType name="personinfo">
</xs:complexType> <xs:sequence>
</xs:element> <xs:group ref="persongroup"/>
</xs:sequence> <xs:element name="country" type="xs:string"/>
</xs:complexType> </xs:sequence>
</xs:element> </xs:complexType>

<persons>
<person>
<full_name>Tove Refsnes</full_name>
<child_name>Hege</child_name> <xs:attributeGroup name="..."> defines a new attribute group
<child_name>Stale</child_name> <xs:attributeGroup ref="..."/> references the group
</person>
</persons>

:: based on W3Schools tutorial :: XML Schema [20/25] :: based on W3Schools tutorial :: XML Schema [21/25]

<any> and <anyAttribute> Elements Element Substitution

The <any> element enables us to extend the XML document with


elements not specified by the schema:

<xs:element name="person"> <xs:element name="name" type="xs:string"/>


<xs:complexType> <xs:element name="nazwisko" substitutionGroup="name"/>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/> Both document fragments are valid:
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/> <!-- so it is optional -->
<name>Smith</name>
</xs:sequence>
<nazwisko>Sobaniec</nazwisko>
</xs:complexType>
</xs:element>

The <anyAttribute> element enables us to extend the XML


document with attributes not specified by the schema

:: based on W3Schools tutorial :: XML Schema [22/25] :: based on W3Schools tutorial :: XML Schema [23/25]
XML Schema Data Types Documentation
xs:string regular string
xs:normalizedString
tabs are replaced with spaces
xs:token XML processor will remove line feeds, carriage returns,
XML Schema is a W3C Recommendation
tabs, leading and trailing spaces, and multiple spaces
xs:date uses notation CCYY-MM-DD XML Schema was originally proposed by Microsoft, but became an
xs:time uses notation hh:mm:ss official W3C recommendation in May 2001.
xs:dateTime uses notation CCYY-MM-DDThh:mm.ss
XML Schema Part 1: Structures.
xs:duration uses notation PnYnMnDTnHnMnS, eg. P5Y2M10D means http://www.w3.org/TR/xmlschema-1/.
5 years, 2 months and 10 days
xs:decimal floating point numbers XML Schema Part 2: Datatypes.
http://www.w3.org/TR/xmlschema-2/.
xs:integer decimal numbers
xs:byte, xs:long, xs:short
other decimal number types
xs:boolean logical
xs:hexBinary, base64Binary, xs:anyURI
other data types
:: based on W3Schools tutorial :: XML Schema [24/25] :: based on W3Schools tutorial :: XML Schema [25/25]

You might also like