Xml schema

PrabhakaranVM1 1,237 views 31 slides Nov 20, 2018
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

XML Schema, Web Technology


Slide Content

V.M.Prabhakaran,
Department of CSE,
KIT- Coimbatore
XML Schema

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.

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

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

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

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.

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

“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

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

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"/>

XSD Example

<?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>

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"/>

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

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".

XSD Restrictions
•Restrictions on Values
•Restrictions on a Set of Values
•Restrictions on a Series of Values
•Restrictions on Whitespace Characters
•Restrictions on Length

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>

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

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>

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>

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>

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>

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>

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>

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>

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>

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>

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

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

Example: Shipping Order
<?xml version="1.0"?>
<shipOrder>

<shipTo>
<name>Svendson</name>
<street>Oslo St</street>
<address>400 Main</address>
<country>Norway</country>
</shipTo>

<items>
<item>
<title>Wheel</title>
<quantity>1</quantity>
<price>10.90</price>
</item>

<item>
<title>Cam</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</items>

</shipOrder>

XML Schema for Shipping Order
<xsd:schema xmlns:xsd=http://www.w3.org/1999/XMLSchema>

<xsd:element name="shipOrder" type="order"/>

<xsd:complexType name="order">
<xsd:element name="shipTo" type="shipAddress"/>
<xsd:element name="items" type="cdItems"/>
</xsd:complexType>

<xsd:complexType name="shipAddress">
<xsd:element name="name“ type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
</xsd:complexType>

XML Schema - Shipping Order (continued)
<xsd:complexType name="cdItems">
<xsd:element name="item" type="cdItem"/>
</xsd:complexType>

<xsd:complexType name="cdItem">
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="quantity“ type="xsd:positiveInteger"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:complexType>

</xsd:schema>