What is the use of DTD in XML?

Abinesh Lal picture Abinesh Lal · Jun 19, 2014 · Viewed 9.3k times · Source

I have write the following XML file with internal DTD.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE welcome [
<!ELEMENT welcome (firstname|lastname)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ENTITY val "abineshlal">
]>
<welcome>
    <middlename>sm</middlename>
    <firstname>&val;</firstname>
    <lastname>welcome</lastname>
</welcome>

I got the output of entity in browser. In my DTD I mentioned only firstname and lastname element in the welcome tag. But I used middle name tag in welcome tag. But I didnt get any error message in output.

I want to the real use of DTD in XML and what it is doing in XML.

Answer

Matthijs picture Matthijs · Jun 19, 2014

From W3Schools:

The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML document.

A DTD defines the document structure with a list of legal elements and attributes.

You can validate your XML-Document to see if it only uses the by you pre-defined tags in your DTD. This takes away the chance of having wrong data in your XML-Document.

It significantly takes down the amount of errors you can receive when using the Xml-Document, as the node-structure is correct after validating and fixing any faults found.

You can use this document to validate.