1
Schema
Introduction
To generate schema we need to follow the bellow
schemata template
<?xml version="1.0"
encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<!—data here Ã
</xs:schema>
|
·
xmlns:xs=http://www.w3.org/2001/XMLSchema
indicates that the elements and datatypes used in schema comes from specified
URL. Name space name should be preferred as “
xs”
·
elementFormDefault="qualified" indicates that the
elements defined in the documents must be qualified by namespace
2 Simple
Element:
Simple types contains the
following elements.
·
Element
·
Attribute
·
restriction
2.1
Element:
It just contains only one text
and it should not contains the other elements or attributes. The text can be of
many different types included in the xml schema definition. We can add the
restriction to limit the content.
2.1.1
Syntax simple element:
<xs:element name="xxx" type="yyy"/>
xxx
is the name of element and yyy is the data
type of the element. Schema has lots of built in data types but most common
data types are
·
xs:string
·
xs:decimal
·
xs:integer
·
xs:date
·
xs:Boolean
·
xs:time
2.1.2
Examples:
Suppose following are the xml
tags and related simple elements
<name>Chandra</name>
|
<xs:element name=”name” type=”xs:string”/>
|
<age>21</age>
|
<xs:element name=”age” type=”xs:integer”/>
|
2.1.3
Other attributes of simple elements:
·
default
·
fixed
Elements can contains the default and fixed
values. Default value means schema will automatically assign the default values
but we change use our own values as well. But fixed means we can’t change the
value of that like a constant
2.2
Attribute:
Simple elements can’t have
attributes if element contains the attribute that means element is complex
element
2.2.1
Syntax of simple element:
<xs:attribute name="xxx" type="yyy"/>
All datatypes are similar to the
simple elements
2.2.2
Examples:
<lastName lang=”EN”>Yen</lastName>
|
<xs:attribute name=”lang” type=”xs:string”/>
|
2.2.3
Other attributes of attribute element:
·
default
·
fixed
·
use
2.3
restrictions:
Used to specify the accepted values for the
xml element and attributes. Restrictions on xml elements are called as facets.
2.3.1
Restrictions on values:
Following is the age restriction
that can’t be lesser than 0 and more than 120
To define this restriction first
we need to create xml tag and related simple element:
2.3.1.1
Xml tag:
<age></age>
2.3.1.2
Simple element:
<xs:element name=”age”
type=”xs:integer”/>
2.3.1.3
Simple element with condition:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
2.3.1.4
Restrictions on set of values:
1
|
<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>
|
2
|
<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>
|
2.3.1.5
Restrictions on series of values:
1
|
One Lower:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
2
|
Allow 3 caps:
<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>
|
3
|
3 lower or upper:
<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>
|
4
|
Only xyz:
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
5
|
5 Digits
<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>
|
6
|
Zero or more occurrence of 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>
|
7
|
Pair of lower and upper case:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
8
|
Acceptable values:
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
9
|
Exactly 8 letters from a to z lower or upper and digits:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
2.3.1.6
Restrictions on white space values:
1
|
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
2
|
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
3
|
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
|
2.3.1.7
Restrictions on length:
<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>
|
2.3.1.8
Restrictions for datatypes:
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 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) is handled
|
3 Complex
elements:
Complex element is an element
that contains the other elements or attributes. There are 4 types of complex
elements
·
Empty element
o
<product pid="1345"/>
·
Element that contains other element
o
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
·
Element that contains text
o
<food type="dessert">Ice cream</food>
·
Element that contains other element or text
o
<description>
It happened on <date lang="norwegian">03.03.99</date> ....
</description>
3.1
Empty element:
Empty complex element can’t have
the contents, it’s just contains the attribute
<product prodid="1345" />
|
<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
|
Or we can divide the above code
into two parts. 1. Simple type and 2. Related complex type
<xs:element name="product" type="prodtype"/>
<xs:complexType name="prodtype">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
|
3.2
Element that contains other elements:
If one element contains the other
element then we can call it as a elements only
<person>
<firstname>John</firstname>
<lastname>Smith</lastname>
</person>
|
To define above elements in
schema we need to define two simple elements and one complex 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:complexType>
</xs:element>
|
Same thing we can define as
bellow
<xs:element name="person" type="persontype"/>
<xs:complexType name="persontype">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
|
3.3
Element that contains text:
This element contains the text
and attributes. This type contains the simple content and so we will add simple
content tag around it. When we use simple content tag we must use the
extinction or restriction tags.
<shoesize country="france">35</shoesize>
|
To convert this tag we need to
use simple content type with extinction
<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
|
3.4
Element that contains other elements and text:
This can contains the attributes,
elements and text. We will call it as a mixed content
To represent the mixed content we
will use the mixed tag to true
<letter>
Dear Mr.<name>John
Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
|
To represent the data in xsd we
will use bellow format
<xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:integer"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
|
4 Indicators
We can control how elements are
to be used in documents with indicators. We have 7 indicators and categorised
as 3 types.
4.1.1
Order indicators:
Order indicators are used to
define the order of elements. We have 3 types of order indicators
·
All
·
Choice
·
Sequence
4.1.1.1
All:
All indicates that the order of
child element can occurs in any order
<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>
|
4.1.1.2
Choice:
Specifies that either one can
occur
<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>
|
4.1.1.3
Sequence:
Specifies that elements can occur
on same way that we have specified
<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:complexType>
</xs:element>
|
4.1.2
Occurrence indicators:
It is defines that how often that
the element can occur. This two types. All group and order indicators default
value of maxOccurs and minOccurs value is 1
·
maxOccurs
·
minOccurs
4.1.2.1
maxOccurs:
Specifies that how many times
that the element can repeat
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="fname" type="xs:string"/>
<xs:element name=”lname" type="xs:string" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
</xs:element>
|
4.1.2.2
minOccurs:
specifies that how many minimum times
that the elements 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>
|
4.1.3
Group indicator:
Indicates the related set of
elements these are two types.
·
group
·
attributeGroup
4.1.3.1
Group:
Indicates the group of declarations.
We must add all or choice or sequence indicator inside the group declaration.
<xs:group name="persongroup">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
</xs:sequence>
</xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:group ref="persongroup"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
|
4.1.3.2
attributeGroup:
Used to define attribute groups.
<xs:attributeGroup name="personattrgroup">
<xs:attribute name="firstname" type="xs:string"/>
<xs:attribute name="lastname" type="xs:string"/>
<xs:attribute name="birthday" type="xs:date"/>
</xs:attributeGroup>
<xs:element name="person">
<xs:complexType>
<xs:attributeGroup ref="personattrgroup"/>
</xs:complexType>
</xs:element>
|
5 Element
substitution:
Let's say that we have users from two
different countries: England and Norway. We would like the ability 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"/>
<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"/>
|
To use this element we will follow bellow code
<customer>
<name>John Smith</name>
</customer>
|
<kunde>
<navn>John Smith</navn>
</kunde>
|
6 Data
types:
6.1
String data type:
ENTITIES
|
ENTITY
|
ID
|
IDREF
|
IDREFS
|
language
|
Name
|
NCName
|
NMTOKEN
|
NMTOKENS
|
normalizedString
|
QName
|
string
|
token
|
6.2
Date datatype:
date
|
dateTime
|
duration
|
gDay
|
gMonth
|
gMonthDay
|
gYear
|
gYearMonth
|
time
|
6.3
Numeric data types:
byte
|
decimal
|
int
|
integer
|
long
|
negativeInteger
|
nonNegativeInteger
|
nonPositiveInteger
|
positiveInteger
|
short
|
unsignedLong
|
unsignedInt
|
unsignedShort
|
unsignedByte
|
6.4
Miscellaneous data type:
anyURI
|
base64Binary
|
boolean
|
double
|
float
|
hexBinary
|
NOTATION
|
QName
|