XML Tutorial menu

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-formedValid
MeaningFollows XML syntax rulesAlso follows a set of structure rules
Needs a DTD or schemaNoYes
Example problem it catchesMissing end tagMissing <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
<?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

SymbolMeaning
(name, age)These children, in this order
+One or more
*Zero or more
?Optional (zero or one)
#PCDATAText
#REQUIREDThe 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
<?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

TypeAccepts
xs:stringAny text
xs:integerWhole numbers
xs:decimalNumbers with decimals
xs:booleantrue, false, 1, 0
xs:dateA 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

bash
xmllint --noout --schema students.xsd students.xml

If a student has <age>nineteen</age>, you get:

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

Learn XML with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp