DTD stands for Document Type Definition. It defines the legal building blocks of an XML document. It is used to define document structure with a list of legal elements and attributes.
Purpose of DTD
Its main purpose is to define the structure of an XML document. It contains a list of legal elements and define the structure with the help of them.
Before proceeding with XML DTD, you must check the validation. An XML document is called “well-formed” if it contains the correct syntax.
A well-formed and valid XML document is one which have been validated against DTD.
Visit http://www.xmlvalidation.com to validate the XML file.
Valid and well-formed XML document with DTD
Let’s take an example of well-formed and valid XML document. It follows all the rules of DTD.
- <?xml version=“1.0”?>
- <!DOCTYPE employee SYSTEM “employee.dtd”>
In the above example, the DOCTYPE declaration refers to an external DTD file. The content of the file is shown in below paragraph.
- <!ELEMENT employee (firstname,lastname,email)>
- <!ELEMENT firstname (#PCDATA)>
- <!ELEMENT lastname (#PCDATA)>
- <!ELEMENT email (#PCDATA)>
Test it Now
Description of DTD
<!DOCTYPE employee : It defines that the root element of the document is employee.
<!ELEMENT employee: It defines that the employee element contains 3 elements “firstname, lastname and email”.
<!ELEMENT firstname: It defines that the firstname element is #PCDATA typed. (parse-able data type).
<!ELEMENT lastname: It defines that the lastname element is #PCDATA typed. (parse-able data type).
<!ELEMENT email: It defines that the email element is #PCDATA typed. (parse-able data type).
XML DTD with entity declaration
A doctype declaration can also define special strings that can be used in the XML file.
An entity has three parts:
- An ampersand (&)
- An entity name
- A semicolon (;)
Syntax to declare entity:
- <!ENTITY entity-name “entity-value”>
Let’s see a code to define the ENTITY in doctype declaration.
- <?xml version=“1.0” standalone=“yes” ?>
- <!DOCTYPE author [
- <!ELEMENT author (#PCDATA)>
- <!ENTITY sj “Sonoo Jaiswal”>
Test it Now
In the above example, sj is an entity that is used inside the author element. In such case, it will print the value of sj entity that is “Sonoo Jaiswal”.