0% found this document useful (0 votes)
83 views3 pages

Validate XML With Schema: Example + Main Syntax

The document provides an example of validating an XML document with a schema. It includes an XML document with college data and a schema that defines the structure and validates the XML. The schema specifies elements like Student and Class, along with their data types and occurrence rules. The second part of the document adds constraints to the schema like limiting major types and class name length. It demonstrates qualifying elements with a namespace and linking the XML and schema.

Uploaded by

Dandoun Dandoun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views3 pages

Validate XML With Schema: Example + Main Syntax

The document provides an example of validating an XML document with a schema. It includes an XML document with college data and a schema that defines the structure and validates the XML. The schema specifies elements like Student and Class, along with their data types and occurrence rules. The second part of the document adds constraints to the schema like limiting major types and class name length. It demonstrates qualifying elements with a namespace and linking the XML and schema.

Uploaded by

Dandoun Dandoun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy