Topic-XML SCHEMA
Name-Prateek Rathore
Roll No-205119068
XML Schema Rules
XML Schemas is all about expressing rules:
Rules about what data is allowed
Rules about how the data must be organized
Rules about the relationships between data
2
Example: sep-9.xml
<Vacation date=“2010-09-09” guide-by=“Lee”>
<Trip segment="1" mode="air">
<Transportation>airplane<Transportation>
</Trip>
<Trip segment="2" mode="water">
<Transportation>boat</Transportation>
</Trip>
<Trip segment="3" mode="ground">
<Transportation>car</Transportation>
</Trip>
</Vacation>
3
Validation
<Vacation date=“2010-09-09” guide-by=“Lee”>
<Segment id="1" mode="air">
<Transportation>airplane</Transportation>
</Segment>
<Segment id="2" mode="water">
<Transportation>boat</Transportation>
</Segment> Validate the XML
<Segment id="3" mode="ground"> document against
<Transportation>car</Transportation> the XML Schema
</Segment>
</Vacation>
nvML.dtd or nvML.xsd XML Schema = RULES
Rule 1: A vacation has segments.
Rule 2: Each segment is uniquely identified.
Rule 3: There are three modes of transportation: air, water, ground.
Rule 4: Each segment has a mode of transportation.
Rule 5: Each segment must identify the specific mode used.
4
Schema Processing
5
Important Schema Concepts
Global declarations are direct children of the root schema
element. They are visible everywhere.
All local declarations are local and are limited in scope to the
element that they appear within.
Value space-:The range of values that the type can take.
Lexical space-:The range literals that represent the value.
Set of facets-:The defining properties of a type.
Fundamental facets include equality, order, bounds,
cardinality, numeric/non-numeric
Constraining facets include ranges for numbers, string lengths,
or a regular expressions
6
Namespaces
XML Namespaces - The xmlns Attribute.
When using prefixes in XML, a namespace for the prefix
must be defined.
The namespace can be defined by an xmlns attribute in
the start tag of an element.
The namespace declaration has the following syntax.
xmlns:prefix="URI".
XML Schema file mixes vocabulary from the XML Schema
language with own vocabulary to be created.
Has to keep both separate using namespaces.
Namespaces associate a URI with names.
7
8
Well-Formed: Not Enough
Well-Formed: a document confirms to XML syntax rules
such as:
Begin with XML declaration.
One unique root
Case-sensitive
Matching Start / End tags
Properly nested
Well-formed documents can still contain semantic
errors or inconsistencies
Need VALID documents according to schema.
9
note.xml
<?xml version="1.0"?>
// Reference to schema goes here
<note>
<to> Aman </to>
<from> Prateek </from>
<heading>Reminder</heading>
<body>----</body>
</note>
10
note.dtd
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
11
note.xml with Reference to
DTD
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"http://www.w3schools.com/dtd/note.dtd">
<note>
<to>Aman</to>
<from>Prateek</from>
<heading>Reminder</heading>
<body>------</body>
</note>
12
note.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs= “http://www.w3.org/2001/XMLSchema”
targetNamespace= “http://www.w3schools.com”
xmlns= “http://www.w3schools.com”
elementFormDefault= "qualified">
<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>
13
<schema> element
<?xml version="1.0"?>
<xs:schema
xmlns:xs = “http://www.w3.org/2001/XMLSchema”
targetNamespace = “http://www.w3schools.com”
xmlns = “http://www.w3schools.com”
elementFormDefault= "qualified">
. . .
</xs:schema>
<schema> element is the root element of every
XML Schema.
14
<schema> element
<?xml version="1.0"?>
<xs:schema
xmlns:xs = “http://www.w3.org/2001/XMLSchema”
targetNamespace = “http://www.w3schools.com”
xmlns = “http://www.w3schools.com”
elementFormDefault= "qualified">
. . .
</xs:schema>
Elements & data types in this schema file come
from http://www.w3.org/2001/XMLSchema
namespace.
They are to be prefixed with “xs:”
15
<schema> element
<?xml version="1.0"?>
<xs:schema
xmlns:xs = “http://www.w3.org/2001/XMLSchema”
targetNamespace = “http://www.w3schools.com”
xmlns = “http://www.w3schools.com”
elementFormDefault= "qualified">
. . .
</xs:schema>
Indicates that the elements defined by this
schema (eg, note, to, from, heading, body.)
come from the target namespace.
16
<schema> element
<?xml version="1.0"?>
<xs:schema
xmlns:xs = “http://www.w3.org/2001/XMLSchema”
targetNamespace = “http://www.w3schools.com”
xmlns = “http://www.w3schools.com”
elementFormDefault= "qualified">
. . .
</xs:schema>
Default namespace
17
<schema> element
<?xml version="1.0"?>
<xs:schema
xmlns:xs = “http://www.w3.org/2001/XMLSchema”
targetNamespace = “http://www.w3schools.com”
xmlns = “http://www.w3schools.com”
elementFormDefault= "qualified">
. . .
</xs:schema>
Any elements used by the XML instance
document which were declared in this schema
must be namespace qualified.
18
note.xml with Reference to
XML Schema
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:schemaLocation="http://www.w3schools.com
note.xsd”>
<to>Aman</to>
<from>Prateek</from>
<heading>Reminder</heading>
<body>-----</body>
</note>
19
note.xml with Reference to
XML Schema
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Default namespace for the “note.xml” file
xsi:schemaLocation="http://www.w3schools.com note.xsd”>
<to>Tove</to>
Tell schema validator that all the elements used
<from>Jani</from>
in “note.xml” file are declared in this namespace
<heading>Reminder</heading>
<body>-----</body>
</note>
20
note.xml with Reference to
XML Schema
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd”>
<to>Tove</to>
<from>Jani</from>
Once the XML Schema Instance namespace is
<heading>Reminder</heading>
available can use schemaLocation attribute
<body>Don't forget me this weekend!</body>
</note>
21
note.xml with Reference to
XML Schema
<?xml version="1.0"?>
<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com
note.xsd”>
<to>Tove</to>
<from>Jani</from>
First value: the namespace to use
<heading>Reminder</heading>
Second value: the name/location of the XML
<body>Don't forget me this weekend!</body>
schema to use for that namespace
</note>
22
<xs:attribute>
An XML element with an attribute:
<lastname lang="EN">Smith</lastname>
Corresponding attribute definition:
<xs:attribute name="lang" type="xs:string"/>
Attributes can have default or fixed values. If the
attribute is required, add use=“required”
23
Confirming to Types
When an XML element or attribute has a
data type defined, it puts restrictions on
the element's or attribute's content.
If an XML element is of type "xs:date" and
contains a string like "Hello World", the
element will not validate.
With XML Schemas, you can also add your
own restrictions to your XML elements and
attributes.
24
Constraining User-Defined Types
Defines an element called "age" with a
restriction
The value of age cannot be lower than 0
or greater than 120
<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>
25
Constraining User-Defined
Types
Defines an element called "car" with a restriction
The only acceptable values are: Audi, Golf, BMW:
<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>
Note: In this case the type "carType" can be used by other
elements because it is not a part of the "car" element.
26
Example-Define a Complex Element
Method 1: no re-use foreseen
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname"
type="xs:string"/>
<xs:element name="lastname“
type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
27
Example-Define a Complex Element
Method 2: can reuse “myInfo” type
<xs:element name="employee” type=“myInfo”>
<xs:complexType name=“myInfo”>
<xs:sequence>
<xs:element name="firstname"
type="xs:string"/>
<xs:element name="lastname“
type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
28
Example-Define a Complex Element
Method 2: 3 elements can reuse “myInfo” type
<xs:element name="employee" type="myInfo"/>
<xs:element name="student" type="myInfo"/>
<xs:element name="member" type="myInfo"/>
<xs:complexType name="myInfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
29