Web Technologies - Old Questions

10.  Why is it important to specify a DTD for an XML document? Explain.

5 marks | Asked in 2068

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.  A DTD can be declared inline inside an XML document, or as an external reference.

It is important to specify the a DTD for an XML document because:

  • It defines the different piece of data planning to model, along with their relationship.
  • With DTD, each of your XML files can carry a description of its own format with it.
  • With a DTD, independent groups of people can agree to use a common DTD for interchanging data.
  • Your application can use a standard DTD to verify that the data you receive from the outside world is valid.
  • You can also use a DTD to verify your own data.

Example of DTD:

<?xml version="1.0"?>  

<!DOCTYPE student [

<!ELEMENT student(firstname,lastname)>  

<!ELEMENT firstname (#PCDATA)>  

<!ELEMENT lastname (#PCDATA)>  

]>

<student>  

  <firstname>Jayanta</firstname>  

  <lastname>Poudel</lastname>  

</student>