Chapter 19: XML Schema

1. What is XML Schema (XSD)? The clearest possible explanation

XML Schema (usually called XSD — XML Schema Definition) is a powerful language that lets you define very precise rules for what an XML document is allowed to look like.

It answers questions like:

  • Which elements are allowed?
  • In what order must they appear?
  • Which elements are required vs optional?
  • What data types are allowed (string, integer, date, decimal, email, etc.)?
  • Which attributes must exist?
  • What are the allowed values (enumerations)?
  • What are the minimum/maximum occurrences?
  • What are the patterns (regex)?
  • Are there any uniqueness or key constraints?

Analogy everyone understands:

Imagine you are building houses (XML documents) for a city council.

  • A DTD is like a very basic building code: “Every house must have a door, windows, and a roof — in that order.”
  • An XML Schema (XSD) is like a modern, detailed construction law:
    • The door must be 90 cm wide and made of wood
    • Windows must be double-glazed
    • At least 2 bedrooms, maximum 5
    • Electricity connection must be 220V, 50Hz
    • Roof must have slope between 30° and 45°
    • No swimming pool allowed on the roof
    • Each house must have a unique plot number

Key advantages over DTD:

Feature DTD XML Schema (XSD)
Data types Almost none Many (integer, decimal, date, email, regex…)
Namespaces No support Full support
Complex rules Very limited Very powerful (sequences, choices, groups…)
Attribute constraints Basic Very detailed
Patterns / regex No Yes
Uniqueness / keys Very limited (ID/IDREF) Full support (unique, key, keyref)
Still used in 2025–2026 Legacy only Dominant standard

2. Basic Structure of an XSD File

Every XSD document looks roughly like this:

XML

Important parts:

  • xmlns:xs=”http://www.w3.org/2001/XMLSchema” → the namespace for schema keywords (xs:element, xs:string, etc.)
  • targetNamespace → the namespace your XML documents should use (very important in real projects)
  • elementFormDefault=”qualified” → most elements must be namespace-qualified

3. Very First Realistic Example – Simple Product Schema

product.xsd

XML

Valid XML document

XML

Invalid examples (will be rejected by validator):

XML

4. Most Important Building Blocks – With Examples

a) Simple Types vs Complex Types

  • Simple type = only text or value (no child elements) → xs:string, xs:integer, xs:decimal, xs:date, xs:boolean…
  • Complex type = can have child elements and/or attributes

b) Common Element Declarations

XML

c) Attributes

XML

d) Patterns (regex)

XML

e) Choice (either-or)

XML

5. Real-World Style Example – Invoice (very typical structure)

invoice.xsd (simplified but realistic)

XML

This is the kind of schema you see in real enterprise, e-invoicing, financial messages, etc.

Quick Summary – XML Schema Cheat Sheet

  • Root = <xs:schema>
  • Define elements with <xs:element name=”…” type=”…”/>
  • Simple types: xs:string, xs:integer, xs:decimal, xs:date, xs:boolean…
  • Complex types = elements + attributes + children
  • minOccurs / maxOccurs control cardinality
  • use=”required” / use=”optional” for attributes
  • pattern, enumeration, minInclusive, maxInclusive for restrictions
  • targetNamespace + elementFormDefault=”qualified” → modern best practice

Would you like to continue with one of these next?

  • Writing a realistic XSD step by step for a specific use case (invoice, order, student report…)
  • XSD with namespaces – how it really works in practice
  • Difference between XSD 1.0 vs XSD 1.1 (assertions, etc.)
  • How to validate XML against XSD (tools, command line, code)
  • Common mistakes when writing XSDs
  • How GST e-invoice schema looks in real life

Tell me which direction feels most useful or interesting for you right now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *