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 children
  • xs:choice: one of many
  • xs: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 library or student.
  • 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.