Structure Lesson 4 of 9
XML Validation — DTD and XML Schema (XSD)
Define the allowed structure of an XML document with a DTD or an XML Schema (XSD) — required elements, data types, attributes and occurrence rules — and understand valid vs well-formed XML.
"Well-formed" means the syntax is correct. Valid means the document also follows an agreed structure: the right elements, in the right order, with the right kinds of values. That structure is described by a DTD or an XML Schema.
Well-formed vs valid
<student>
<name>Ana Reyes</name>
<grade>ten</grade>
</student>
This is well-formed (the syntax is fine). But if the agreement says grade must be a number from 7 to 12, it's not valid. Validation catches these problems before bad data enters a system.
DTD (Document Type Definition)
The older, simpler way to describe structure:
<?xml version="1.0"?>
<!DOCTYPE classlist [
<!ELEMENT classlist (student+)>
<!ELEMENT student (name, grade, email?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT grade (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ATTLIST student lrn CDATA #REQUIRED>
]>
<classlist>
<student lrn="136728140001">
<name>Ana Reyes</name>
<grade>10</grade>
</student>
</classlist>
| Symbol | Meaning |
|---|---|
(name, grade) | These children, in this order |
+ | One or more |
* | Zero or more |
? | Optional (zero or one) |
#PCDATA | Text content |
#REQUIRED / #IMPLIED | Attribute is required / optional |
You recognize <!DOCTYPE html> from HTML: it's the descendant of this idea. DTDs can't say "grade must be a number", which is why XML Schema was created.
XML Schema (XSD)
An XSD is itself written in XML and supports real data types:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="classlist">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="grade">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="7"/>
<xs:maxInclusive value="12"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="birthDate" type="xs:date" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="lrn" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{12}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This schema requires: one or more students; each with a name, a grade that's an integer from 7 to 12, an optional birthDate in YYYY-MM-DD format, and an LRN attribute of exactly 12 digits.
An XML file points to its schema like this:
<classlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="classlist.xsd">
...
</classlist>
Common XSD types
| Type | Example value |
|---|---|
xs:string | Ana Reyes |
xs:integer, xs:decimal | 10, 91.50 |
xs:boolean | true, false |
xs:date, xs:dateTime | 2026-12-02, 2026-12-02T07:30:00 |
How to validate
- VS Code with an XML extension validates against a schema as you type.
- Programming languages (Java, C#, Python with lxml, PHP's DOMDocument) can validate before importing data.
- Online XML validators let you paste XML and XSD to check them.
Browsers check only well-formedness, not schemas.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04