Validate XML
With
Schema
Example + main Syntax:
Given the following xml document:
College.xml
<?xml version=“1.0” encoding=“utf-8” standalone=“yes” ?>
<College>
<Student>Rida</Student>
<Student>Nisrin</Student>
<Class>
<Major>Computer</Major>
<Name>BT1</Name>
</Class>
<Class>
<Major>Computer</Major>
<Name>BT2</Name>
</Class>
</College>
Write the necessary schema to validate the previous xml
Solution:
College.xsd
<?xml version=“1.0” encoding=“utf-8” standalone=“yes” ?>
<schema xmlns=“http://www.w3.org/2001/XMLSchema”
version=“1.0”>
<element name=“College”>
<complexType>
<sequence>
<element name=“Student” type=“string” minOccurs= “1” maxOccurs= “unbounded”/>
<element name=“Class” minOccurs=“1” maxOccurs=“unbouned”>
<complexType>
<sequence>
<element name=“Major” type=“string” />
<element name=“Name” type=“string” />
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
Exercise #2-
Given the following xml document:
College.xml
<?xml version=“1.0” encoding=“utf-8” standalone=“yes” ?>
<College xmlns=“http://www.ciscollege.edu.lb”>
<Student>Rida</Student>
<Student>Nisrin</Student>
<Class>
<Major>Computer</Major>
<Name>BT1</Name>
</Class>
<Class>
<Major>Computer</Major>
<Name>BT2</Name>
</Class>
</College>
Write the necessary schema to validate the previous xml
Follow these constraints:
The Major could be one of the followings: Computer, Accounting, Hotel
The length of the class name, must not exceed 10 letters
Student and Class elements could be written in any order
Write the schema with prefix qualification
Write the link statement between xml and schema
<?xml version=“1.0” encoding=“utf-8” standalone=“yes” ?>
<xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema”
targetNamespace=“http://www.ciscollege.edu.lb”
elementFormDefault=“qualified”
version=“1.0”>
<xs:element name=“College”>
<xs:complexType>
<xs:all>
<xs:element name=“Student” type=“string” minOccurs= “1” maxOccurs= “unbounded”>
<xs:element name=“Class” minOccurs= “1” maxOccurs= “unbounded”>
<xs:complexType>
<xs:sequence>
<xs:element name=“Major”>
<xs:simpleType>
<xs:restrition base=“xs:string”>
<xs:enumeration value=“Computer” />
<xs:enumeration value=“Accounting” />
<xs:enumeration value=“Hotel” />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name=”Name”>
<xs:simpleType>
<xs:restrition base=“xs:string”>
<xs:maxLength value=“10” />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
The link statement:
This is because the default xml document has a qualification to ciscollege.edu.lb
<?xml version="1.0" encoding="UTF-8"?>
<College xmlns="http://www.ciscollege.edu.lb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ciscollege.com college.xsd"
. . .
</College>
Suppose the xml document doesn’t have a qualification so, the link statement is going to
be:
<?xml version="1.0" encoding="UTF-8"?>
<College xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="college.xsd"
. . .
</College>