Home >>XML Tutorial >XML DTD



What is DTD

DTD stands for description of type of document. It specifies the legal structure of an XML text. document structure is specified with a set of legal elements and attributes.

Purpose of DTD

The principal aim is to describe the XML document structure. This includes a set of legal elements and with their help, describes the structure.

Checking Validation

You'll need to check the validation before continuing with XML DTD. If an XML document includes the correct syntax, it is considered "well-formed."

A well-formed, valid XML document is one that was 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 illustration of an XML document well-formed and real. It follows any of the DTD laws.


<?xml version="1.0"?>  
<!DOCTYPE employee SYSTEM "employee.dtd">  
  <email>[email protected]</email>  

For the case above the DOCTYPE declaration applies to an external DTD file. The file contents are shown in paragraph below.


<!ELEMENT employee (firstname,lastname,email)>  
<!ELEMENT firstname (#PCDATA)>  
<!ELEMENT lastname (#PCDATA)>  
<!ELEMENT email (#PCDATA)>  

Description of DTD

<!DOCTYPE employee :It determines employee to be the root aspect of the document.

<!ELEMENT employee:It determines that the employee variable includes 3 "first name , last name and e-mail" elements.

<!ELEMENT firstname:It determines the first name element to be typed # PCDATA. (Type of data which is parse-able).

<!ELEMENT lastname:It determines that # PCDATA is typed with the last name element. (Type of data which is parse-able).

<!ELEMENT email:It determines that # PCDATA is typed in the email element. (Type of data which is parseable).

XML DTD with entity declaration

A doctype declaration may also describe unique strings which can be included in XML file.

There are three sections of an entity:

  1. An ampersand (&)
  2. An entity name
  3. 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">