Lecture 7
Lecture 7
</xs:schema>
XML Schema (XSD)
• An XML schema is an element with an
opening tag like
<schema
"http://www.w3.org/2000/10/XMLSchema"
version="1.0">
• Structure of schema elements
– Element and attribute types using data types
Association of XSD with XML
Association of your XSD with XML
You must declare the XMLSchema-instance namespace.
The xsi:noNamespaceSchemaLocation attribute defines the URL of your
XSD
XSD file: recipe-no-ns.xsd
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="list">
XML: recipe-no-ns.xml
<?xml version="1.0" ?>
<list
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="recipe-no-ns.xsd">
<recipe> ....
</list>
Association of XSD with XML
Simple Type
A Simple types consists of :
• Element: It contains simple element as well as other element
content.
• Attribute: It’s a type used in Simple data types.
• Restriction: It has acceptable values in the respective XML
document.
XML Schema Data types
Simple Type
• Element
• A simple element is defined as follows
<xs : element name = “ hgfhj” type = “ xxxx” />
• An element declaration is an association of a name with a type
definition
• The name property and holds the description about the element.
The type includes few pre-defined types like xs : integer, xs : date,
xs : string, xs : Boolean.
• The appearance of an element is defined by a cardinality which
are specified using attributes like minOccurs and maxOccurs.
XML Schema Data types
Simple Type
• Element
<xsd:element name="Description" type="xs:string"/>
<xsd:element name="Price" type="xs:decimal"/>
<xsd:element name="Quantity" type="xs:integer"/>
XML Schema Data types
Simple Type
• Element
<element name="email"/>
<element name="head“ minOccurs="1“ maxOccurs="1"/>
<element name="to" minOccurs="1"/>
Cardinality constraints:
– minOccurs="x" (default value 1)
– maxOccurs="x" (default value 1)
– Generalizations of *,?,+ offered by DTDs
XML Schema Data types
Simple Type
• Restriction
• The elements which have some restrictions are called facets.
• It defines the accepting elements.
• It takes data types like enumeration, length, max inclusive,
exclusive.
<xs:element name="bookno">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[1-8 ][1-8][1-8][1-8]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
XML Schema Data types
Simple Type
Restriction
<xsd:element name="element name">
<xsd:simpleType>
<xsd:restriction base="xsd: Integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
XML Schema Data types
Schema Datatype Restrictions
Constraint Description
enumeration Defines a list of acceptable values
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
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
pattern Defines the exact sequence of characters that are acceptable
XML Schema Data types
Restriction
<xsd:element name="Size">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="small"/>
<xsd:enumeration value="medium"/
<xsd:enumeration value="large"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
XML Schema Data types
Simple Type
• Attributes
The attribute by itself declares it has simple types. If an element
has specified attributes then it is a complex type.
Syntax of attribute:
<xs : attribute name =” order” type =” string/integer/Boolean”/>
XML document with attribute.
<product id = “2123” > Body Wash </product>
XSD line for the above XML document.
<xs :attribute name =” product “ type =xs :int”/>
If attributes are needed then it is important to assign the keyword
‘use’ to the statement.
<xs :attribute name =” id “ type =xs :int” use=”required” />
XML Schema Data types
• Complex data types
• Simple elements only contain data, complex
elements can hold much more.
• Complex elements can contain attributes,
child elements, and additional indicators that
focus on the order of child elements and how
often they can occur.
XML Schema Data types
Complex data types
are defined from existing data types by defining some attributes (if
any) and using:
– sequence, a sequence of existing data type elements (order is
important)
– all, a collection of elements that must appear (order is not
important)
– choice, a collection of elements, of which one will be chosen(Only
one child element from the list can appear)
XML Schema Data types
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author" type="xs:string" />
<xs:element name="isbn" type="xs:integer" />
<xs:element name="genre" type ="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
Comparison between DTD and XML schema
Features DTD XML Schema
Powerful (44 + your own derived
Data types Little (10, mostly attribute values)
data types)
Overall complexity low high
Association with XML document DOCTYPE declaration Namespace declaration
File suffix *.dtd *.xsd