Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

XML DTD


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.


Checking Validation

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.

employee.xml

  1. <?xml version=“1.0”?>
  2. <!DOCTYPE employee SYSTEM “employee.dtd”>
  3. <employee>
  4.   <firstname>vimal</firstname>
  5.   <lastname>jaiswal</lastname>
  6.   <email>vimal@Kreationnext.com</email>
  7. </employee>

In the above example, the DOCTYPE declaration refers to an external DTD file. The content of the file is shown in below paragraph.

employee.dtd

  1. <!ELEMENT employee (firstname,lastname,email)>
  2. <!ELEMENT firstname (#PCDATA)>
  3. <!ELEMENT lastname (#PCDATA)>
  4. <!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:

  1. An ampersand (&)
  2. An entity name
  3. A semicolon (;)

Syntax to declare entity:

  1. <!ENTITY entity-name “entity-value”>

Let’s see a code to define the ENTITY in doctype declaration.

author.xml

  1. <?xml version=“1.0” standalone=“yes” ?>
  2. <!DOCTYPE author [
  3.   <!ELEMENT author (#PCDATA)>
  4.   <!ENTITY sj “Sonoo Jaiswal”>
  5. ]>
  6. <author>&sj;</author>

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”.

Note: A single DTD can be used in many XML files.

Comments

comments