Chapter3 CEF482
Chapter3 CEF482
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:
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:schema>
Strong Typing: You can define types like xs:integer, xs:date, xs:boolean, which
ensures data is correctly formatted.
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.
<price>29.99</price>
Simple types are atomic values like strings, numbers, and dates. They don't contain attributes or child
elements.
Example:
Complex Types
Complex types are used when elements can contain other elements or attributes.
Example:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
This defines an <employee> element with two child elements and one attribute.
Example:
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<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:sequence>
</xs:complexType>
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:
Real-life Analogy:
XSD Definition
<xs:element name="phoneNumbers">
<xs:complexType>
<xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
Valid XML
<phoneNumbers>
<phoneNumber>+237620000000</phoneNumber>
<phoneNumber>+237650000000</phoneNumber>
</phoneNumbers>
<phoneNumbers>
<phoneNumber>+237620000000</phoneNumber>
<phoneNumber>+237650000000</phoneNumber>
<phoneNumber>+237670000000</phoneNumber>
</phoneNumbers>
XSD Definition
This means the <middleName> element can be omitted from the XML.
Valid XML
<person>
<firstName>James</firstName>
<lastName>Smith</lastName>
</person>
Exercices
Define an XML Schema element <phoneNumber> that accepts only digits and has a fixed length of 10
characters.
a title (string)
an author (string)
a price (decimal)
an ISBN attribute (string)
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.
A required <name>
Between 1 and 5 <course> elements