Document Type Definition (DTD)
Another important part of XML is the Document Type Definition (DTD), a file associated with SGML and XML documents that defines how markup tags should be interpreted by the application reading the document. A DTD is what turns XML from a metalanguage to a true language designed for a specific task.
A DTD is a text document that contains a set of rules, formally known as “entity, element, and att-list (attribute) declarations,” that define an XML markup language. It names new elements and describes the type of data or other elements that an element may contain. It also lists attributes for each element.
A Simple DTD
For example, if you were creating recipes to be accessed over the
Web, you might create your own language called RML, or Recipe Markup
Language. RML would have tags like <title>
and <body>, but also RML-specific tags such
as <ingredients>,
<prep-time>, and
<nutritionalInformation>.
These tags would be established in a DTD for the new language. The DTD imparts detailed information about what data should be found in each tag. A DTD for Recipe Markup Language might have a line like this:
<!ELEMENT ingredients ( li+, text? )>
The first line declares an element called
ingredients. An ingredients tag
can contain an li element and text. The plus sign
(+) after li indicates that an
ingredients element will have one or more
li elements within it. The question mark after
text shows that text is optional. The Recipe Markup Language DTD would also specify ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access