XML Validation: DTD and Schema
Validation checks that an XML document has the right structure: the right elements, in the right order, with the right kind of data.
Well-formed vs Valid
| Well-formed | Valid | |
|---|---|---|
| Meaning | Follows XML syntax rules | Also follows a set of structure rules |
| Needs a DTD or schema | No | Yes |
| Example problem it catches | Missing end tag | Missing <price> element, text in a number field |
Every valid document is well-formed, but a well-formed document is not always valid.
Imagine a school and a software house exchanging student records. They agree on rules first: each <student> must have a <name> and an <age>, and age must be a number. Validation checks every file against those rules automatically.
DTD (Document Type Definition)
A DTD is the older, simpler way to describe the structure. It can be written inside the document:
Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students [
<!ELEMENT students (student+)>
<!ELEMENT student (name, age, course?)>
<!ATTLIST student id ID #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT course (#PCDATA)>
]>
<students>
<student id="s1">
<name>Ali Raza</name>
<age>19</age>
<course>SEO</course>
</student>
<student id="s2">
<name>Ayesha Khan</name>
<age>21</age>
</student>
</students>DTD symbols
| Symbol | Meaning |
|---|---|
(name, age) | These children, in this order |
+ | One or more |
* | Zero or more |
? | Optional (zero or one) |
#PCDATA | Text |
#REQUIRED | The attribute must be present |
The DTD can also live in its own file and be linked with <!DOCTYPE students SYSTEM "students.dtd">.
XML Schema (XSD)
XML Schema is the modern way. It is written in XML itself, and it understands data types like integers, dates and decimals. A DTD cannot say "age must be a number"; a schema can.
students.xsd:
Example
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="14"/>
<xs:maxInclusive value="60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="course" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>This schema says: the root is <students>, it has one or more <student> elements, each with a name, an age between 14 and 60, an optional course, and a required id.
Common XSD types
| Type | Accepts |
|---|---|
xs:string | Any text |
xs:integer | Whole numbers |
xs:decimal | Numbers with decimals |
xs:boolean | true, false, 1, 0 |
xs:date | A date like 2026-10-15 |
Checking a File
Many editors (VS Code with an XML extension) validate as you type. You can also validate from the command line with xmllint:
Example
xmllint --noout --schema students.xsd students.xmlIf a student has <age>nineteen</age>, you get:
students.xml:5: element age: Schemas validity error : Element 'age': 'nineteen' is not a valid value of the atomic type 'xs:integer'. students.xml fails to validate
Use XML Schema for new work. You will still meet DTDs in older systems and in the <!DOCTYPE html> line of HTML, which comes from them.
Exercise
Exercise
What can an XML Schema check that a DTD cannot?
