XML Schema
XML Schema
Name-Prateek Rathore
Roll No-205119068
XML Schema Rules
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>
5
Important Schema Concepts
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
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>
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>
<xs:schema
xmlns:xs = “http://www.w3.org/2001/XMLSchema”
targetNamespace = “http://www.w3schools.com”
xmlns = “http://www.w3schools.com”
elementFormDefault= "qualified">
. . .
</xs:schema>
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>
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>
<lastname lang="EN">Smith</lastname>
23
Confirming to Types
24
Constraining User-Defined Types
<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>
26
Example-Define a Complex Element
<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
<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