XSD Fundamentals: Namespaces, Types, and Structure

Unit 3CLO02

Learning Objectives

Course Learning Outcomes

CLO02

Course Outcomes

CO03
ℹ️

Introduction

XSD is the W3C recommendation for expressing XML structure and datatypes using XML syntax itself. It brings namespaces, strong typing, and precise control over order and cardinality.

Study tracker

Mark what you have completed for this topic.

0% done

The Basics

Why XSD beats DTD

  • Written in XML, so it can be parsed and validated like any other XML.
  • Rich built-in types: string, boolean, decimal, date, dateTime, integer, etc.
  • Namespaces are first-class, preventing collisions in large vocabularies.
  • Facets add business rules (length, pattern, ranges).

Technical Details

Skeleton to memorize

<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>

Model groups

  • xs:sequence → ordered children.
  • xs:choice → one of the listed children.
  • xs:all → any order, each 0 or 1 by design.

Occurrence constraints

minOccurs / maxOccurs mirrors ?, *, + from DTD.

Examples

Repeating element example

<xs:element name="book" type="BookType" minOccurs="0" maxOccurs="unbounded"/>

Attribute example

<xs:attribute name="status" type="xs:string" use="optional"/>

Self-check

Real-World Use

Build it

  • Write an XSD for a student or inventory dataset.
  • Add one required attribute and one repeating child.
  • Validate at least two XML instances against it.

📝 For exams

Exam must-knows

  • Role of targetNamespace and elementFormDefault.
  • Difference between simpleType and complexType.
  • When to use sequence vs choice vs all.

✨ Key points

Takeaways

  • Start with a clear namespace; keep it consistent across XML and XSD.
  • Choose the right model group to match business order requirements.
  • Occurrence constraints are your first line of validation.