XSD Fundamentals: Namespaces, Types, and Structure
Unit 3âĸCLO02
Learning Objectives
Course Learning Outcomes
CLO02
Course Outcomes
CO03
âšī¸
Introduction
XML Schema (XSD) is the W3C-recommended language for describing XML structure and constraints. XSD is more expressive than DTD: it is XML-based, supports namespaces properly, and provides rich datatypes and restriction mechanisms.
The Basics
Why XSD?
XSD improves on DTD by adding:
- XML syntax (schemas are XML documents)
- namespaces support
- rich data types (integer, date, decimal, boolean)
- constraint facets (pattern, enumeration, ranges)
Technical Details
Core schema skeleton
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/student"
xmlns="http://example.com/student"
elementFormDefault="qualified">
<xs:element name="student" type="StudentType"/>
<xs:complexType name="StudentType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="dept" type="xs:string"/>
<xs:element name="cgpa" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:schema>
sequence vs choice vs all
xs:sequence: ordered childrenxs:choice: one of manyxs:all: any order (with restrictions)
Examples
Example: repeating element
<xs:element name="book" type="BookType" minOccurs="0" maxOccurs="unbounded"/>
Real-World Use
Practical
- Write an XSD for
libraryorstudent. - Include one repeating element and one required attribute.
đ For exams
Exam
- Define XSD and explain
targetNamespace. - Explain simple vs complex types.
⨠Key points
Takeaways
- XSD is namespace-aware and typed.
- Learn core constructs: types, sequence/choice, occurrences.