0% found this document useful (0 votes)
11 views7 pages

Chapter3 CEF482

The document provides an overview of XML Schema Definition (XSD), detailing its structure, advantages over DTDs, and how it validates XML documents. It explains the concepts of simple and complex types, constraints like minOccurs and maxOccurs, and offers examples for creating reusable components. Additionally, it includes exercises for practical application of XSD concepts.

Uploaded by

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

Chapter3 CEF482

The document provides an overview of XML Schema Definition (XSD), detailing its structure, advantages over DTDs, and how it validates XML documents. It explains the concepts of simple and complex types, constraints like minOccurs and maxOccurs, and offers examples for creating reusable components. Additionally, it includes exercises for practical application of XSD concepts.

Uploaded by

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

School year 2024-2025/ Second semester/ FET / Computer Engineering

XML and Document Content Validation

Chapter 3: XML Schema Definition (XSD)

I. Introduction to XML Schema Definition (XSD)

XML Schema Definition (XSD) is a powerful way to define the structure, content, and
semantics of XML documents. Unlike Document Type Definitions (DTDs), XSD is written in
XML syntax itself, which makes it easier to parse, extend, and integrate with modern XML
tools.

XSD specifies:

 What elements and attributes are allowed

 Their data types (string, integer, date, etc.)

 Their hierarchical relationships

 Rules such as occurrence constraints (e.g., how many times an element can appear)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="person" type="xs:string"/>

</xs:schema>

This schema defines a single element <person> that contains a string.

II. Advantages of Using XML Schema for Validation


Using XML Schema provides multiple benefits over traditional validation approaches like
DTDs:

 Strong Typing: You can define types like xs:integer, xs:date, xs:boolean, which
ensures data is correctly formatted.

 Namespace Support: XSD supports XML namespaces, which allows combining


elements from different vocabularies.

Proposed by Dr. SOP DEFFO Lionel Landry Page 1


School year 2024-2025/ Second semester/ FET / Computer Engineering

 Custom Data Types: You can define your own types based on standard types with
added constraints.

 Reusability: Complex elements and attributes can be reused across the schema.

 Better Tool Support: Most modern XML editors and parsers support XSD validation.

Example: Validating an XML Document

Suppose you have this XML:

<price>29.99</price>

An XSD can validate this as a decimal:

<xs:element name="price" type="xs:decimal"/>

If someone enters text like <price>twenty</price>, validation will fail.

III. Complex Types and Simple Types in XML Schema


Simple Types

Simple types are atomic values like strings, numbers, and dates. They don't contain attributes or child
elements.

Example:

<xs:element name="age" type="xs:integer"/>

Complex Types

Complex types are used when elements can contain other elements or attributes.

Example:

Proposed by Dr. SOP DEFFO Lionel Landry Page 2


School year 2024-2025/ Second semester/ FET / Computer Engineering

<xs:element name="employee">

<xs:complexType>

<xs:sequence>

<xs:element name="name" type="xs:string"/>

<xs:element name="age" type="xs:integer"/>

</xs:sequence>

<xs:attribute name="id" type="xs:string"/>

</xs:complexType>

</xs:element>

This defines an <employee> element with two child elements and one attribute.

IV. Defining Elements and Attributes Constraints, Creating


Reusable Components

Element and Attribute Constraints

You can apply constraints like:

 minOccurs / maxOccurs – to limit occurrences.


 enumeration – to restrict values.
 pattern – to define regex-based value formats.

Example:

<xs:element name="gender">

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:enumeration value="Male"/>

Proposed by Dr. SOP DEFFO Lionel Landry Page 3


School year 2024-2025/ Second semester/ FET / Computer Engineering

<xs:enumeration value="Female"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Reusable Components

You can define types globally and reuse them across multiple elements using the type attribute.

Example:

<xs:complexType name="addressType">

<xs:sequence>

<xs:element name="street" type="xs:string"/>

<xs:element name="city" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:element name="billingAddress" type="addressType"/>

<xs:element name="shippingAddress" type="addressType"/>

V. Understanding minOccurs and maxOccurs in XML Schema

In XML Schema, minOccurs and maxOccurs are attributes used to define how many times
an element is allowed to appear within its parent element.

These constraints are crucial when modeling repeating data, or when indicating whether an
element is optional or mandatory.

Key Concepts:

 minOccurs: The minimum number of times an element must appear.

Proposed by Dr. SOP DEFFO Lionel Landry Page 4


School year 2024-2025/ Second semester/ FET / Computer Engineering

 maxOccurs: The maximum number of times an element can appear.

If not specified, both default to 1.

Real-life Analogy:

Let’s say you are modeling a shopping cart in an online store.

 A cart must contain at least one item → minOccurs="1"


 But you may want to limit it to a maximum of 10 items → maxOccurs="10"

Now let’s apply this in XML Schema.

Example 1: A List of Phone Numbers

You want to allow between 1 and 3 phone numbers for a person

XSD Definition

<xs:element name="phoneNumbers">

<xs:complexType>

<xs:sequence>

<xs:element name="phoneNumber" type="xs:string" minOccurs="1" maxOccurs="3"/>

</xs:sequence>

</xs:complexType>

</xs:element>

Valid XML

<phoneNumbers>

<phoneNumber>+237620000000</phoneNumber>

<phoneNumber>+237650000000</phoneNumber>

Proposed by Dr. SOP DEFFO Lionel Landry Page 5


School year 2024-2025/ Second semester/ FET / Computer Engineering

</phoneNumbers>

Invalid XML (Too many phone numbers)

<phoneNumbers>

<phoneNumber>+237620000000</phoneNumber>

<phoneNumber>+237650000000</phoneNumber>

<phoneNumber>+237670000000</phoneNumber>

<phoneNumber>+237690000000</phoneNumber> <!-- Invalid -->

</phoneNumbers>

Example 2: Optional Element with minOccurs=0

Let’s say the <middleName> of a person is optional.

XSD Definition

<xs:element name="middleName" type="xs:string" minOccurs="0"/>

This means the <middleName> element can be omitted from the XML.

Valid XML

<person>

<firstName>James</firstName>

<!-- middleName is missing, which is okay -->

<lastName>Smith</lastName>

</person>

Special Case: maxOccurs="unbounded"

If you want an element to appear as many times as needed, use:

<xs:element name="comment" type="xs:string" maxOccurs="unbounded"/>

This allows 0, 1, or many <comment> elements in the XML.

Proposed by Dr. SOP DEFFO Lionel Landry Page 6


School year 2024-2025/ Second semester/ FET / Computer Engineering

Exercices

Exercice 1: Simple Type Creation

Define an XML Schema element <phoneNumber> that accepts only digits and has a fixed length of 10
characters.

Exercice 2: Complex Type

Write an XSD fragment for an element <book> that includes:

 a title (string)
 an author (string)
 a price (decimal)
 an ISBN attribute (string)

Exercice 3: Constraint Practice

Create an XSD element <status> that only accepts the values “Active”, “Inactive”, or “Pending”.

Exercice 4: Reusability

Define a reusable personType complex type with name and email, then use it in both <sender> and
<receiver> elements.

Exercise 5: Create an XSD for a <student> element with:

 A required <name>
 Between 1 and 5 <course> elements

Proposed by Dr. SOP DEFFO Lionel Landry Page 7

You might also like