DTD stands for document definition. DTD is the Smarkup language (SGML, XML, HTML) technique used to define a structure of XML document. XML – Elements, Attributes, Entity reference functionalists are define in DTD.
It is a text-based document and its extension is dot dtd (.dtd).
It contains deceleration of XML languages elements, attributes, and entity references. You will learn about how to declare this in the tutorial.
Types of HTML DTD
You can define DTD in 2 ways.
- Internal DTD
- External DTD
DTD examples and syntax
To declare any element, attribute, or entity references need a declaration syntax “<! >”. Let’s see one by one syntax and example of it.
Basic syntax of a DTD is as follows:-
<!DOCTYPE element DTD identifier [ declaration1 declaration2 ........ ]>
Element declaration syntax
For element-name, you can choose as application requirements. content-model describes a data type – text, empty or mix content or child element. So basically allowed the element type of data or child element.
<! ELEMENT element-name (content-model)>
Let’s see one example.
An element-name is “employee“, child elements are (empno, name, salary). Child elements have also required a declaration in this DTD. #PCDATA is a data type that allows only text data and used only form elements.
PCDATA stands for Parsed Character Data.
In this example defining a 4 elements, where 3 are child elements.
<?xml version="1.0"?> <!DOCTYPE employees [ <!ELEMENT employees (empyno,name,salary)> <!ELEMENT empyno (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT salary (#PCDATA)> ]>
XML for upper DTD (XML elements are user define elements and structure are define in DTD).
<employees> <empyno> 90909 </empyno> <name>John</name> <salary>480900</salary> </employees>
In the content-model 5 type of content-type possibles:
- Text-only element – Only allowed text data. Both String and int (number) are considered text data. For that, you can use #PCDATA.
- Child element- A element inside any element called child element. A child-only element can have a child element. There should be no other type of element in this element.
- Empty element
- Any element
How to map DTD to XML?
As upper said you can define internally or externally in xml. You have to use <!DOCTYPE> declaration.
- Internal DTD
If you writing a DTD within an XML document. Let’s see an example of it. It’s only specific to this XML document and it’s not reusable. So that’s why always recommended to use External DTD.
<?xml version="1.0"?> <!DOCTYPE employees [ <!ELEMENT note (empno,name,salary)> <!ELEMENT empno (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT salary (#PCDATA)> ]> <employees> <empno>12345</empno> <name>John</name> <salary>54000</salary > </employees>
- External DTD
If you writing DTD into another document and linked to XML document.
- XML document with a reference to an external DTD
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "employs.dtd"> <employees> <empno>12345</empno> <name>John</name> <salary>54000</salary > </employees>
And here is the file “employs.dtd”, which contains the DTD:
<!ELEMENT note (empno,name,salary)> <!ELEMENT empno (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT salary (#PCDATA)>
What are the Advantages of DTD?
- Documentation − First you can define your own format of the XML file as per application requirements. It will help to understand the structure of the data.
- Validation − By it, you can validate data. For example, checking whether the elements appear in the right order? OR mandatory elements and attributes are in place.
Do comment if you have any doubt and suggestion on this tutorial.