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.
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.
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"> <employee> <firstname>vimal</firstname> <lastname>jaiswal</lastname> <email>email@example.com</email> </employee>
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)>
<!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).
A doctype declaration can also define special strings that can be used in the XML file.
An entity has three parts:
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"> ]> <author>&sj;</author>
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".