# What Is a DTD File and How Do You Write One?
NicolasBrondinBernard
Learn how to write a validation file for your XML files.

Article published on 19/08/2024, last updated on 10/08/2026
To understand the following article, you need to be familiar with the XML format. If that's not the case, it is recommended to read this article beforehand.
A DTD file (for "Document Type Definition") is, as its name indicates, a document in which is precisely described the data structure to follow so that the content of an XML document is valid.
A small semantic point, we talk about an XML document being:
- "well-formed" when it respects the syntax of the language
- "valid" when the content respects the schema provided in the DTD
An example of DTD
Here is an example of a simplified DTD document, but containing several variations to have a complete schema:
<!ELEMENT school (course*)>
<!ELEMENT course (title, description, teacher)>
<!ELEMENT teacher EMPTY>
<!ATTLIST teacher name PCDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT description (#PCDATA)>
And here is an example of valid XML data with respect to the previous schema:
<school>
<course>
<teacher name="John Doe />
<title>Learn JavaScript 101</title>
<description>...</description>
</course>
</school>
Adding a schema to an XML file
For the content of an XML file to be validated against a DTD file, a <!DOCTYPE> tag must be present in the document, just below the header, like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE example SYSTEM "example.dtd">
Having a DTD schema is of course optional, the structure of an XML file can be left completely free (apart from respecting the syntax of the language).
Let's now see what kind of information our famous DTD file contains.

The syntax of a DTD
Defining an element
To define a new valid element, we need three things:
- The declaration
<!ELEMENT ... > - An element name
- The list of child elements (hierarchy)
Example:
<!ELEMENT course (title, description, teacher)>
We know that our course element must contain a title, a description, and a teacher. We just need to declare our child elements, and their own hierarchy to have a complete schema!
Defining hierarchy and multiplicity
When defining the list of child elements of a parent element, several possibilities are available to us. Let's take the example of our school:
A single child
<!ELEMENT school (course)>
Here, we indicate that a school will need to contain a single course.
An optional child
<!ELEMENT school (course?)>
Thanks to the ? operator, we indicate that our school can have between 0 and 1 course.
Several children (with no minimum)
<!ELEMENT school (course*)>
The * operator is the equivalent of a list, a school can have an unlimited number of courses.
Several children (at least 1)
<!ELEMENT school (course+)>
With +, we again have the possibility of having a list, but which must contain at least one child.
Primitive values
In XML, there is only one kind of primitive value: text.
But there are two types for declaring a text value:
#PCDATAfor text where special characters (<,>,&) will be interpreted as being XML#CDATAfor plain text
In our example, the title of a course will simply be text, so the element is declared as follows:
<!ELEMENT title (#PCDATA)>
An empty element
It is also possible to declare an element that will have no child element, referred to as an "empty" element:
<!ELEMENT teacher EMPTY>
Defining an empty element will allow us to store all the element's data directly in attributes.
Defining attributes
To add attributes to an element, simply declare an attribute list linked to our base element, then fill in this list:
<!ELEMENT teacher EMPTY>
<!ATTLIST teacher firstname PCDATA #REQUIRED>
<!ATTLIST teacher lastname PCDATA #REQUIRED>
<!ATTLIST teacher age PCDATA #REQUIRED>
Find all the types and options of attributes in the documentation: https://www.w3schools.com/xml/xml_dtd_attributes.asp
To note
The DTD can be inserted directly into the XML file it validates.
This is then called an "inline" DTD, as below:
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
This method has two major drawbacks:
- It makes your XML file heavier
- The DTD cannot be reused to validate another file
Complete courses, exercises and certificates to really learn programming!
4.8 average rating
No comments yet