XML DTD – Document Type Definition

To define the valid XML , DTD is being used. The Tutorial provides the details on how to Validate XML using DTD.

What is DTD in XML ?

DTD in XML means Document Type Definition which defines the XML Structure for the XML elements in the XML Document. A well -formed or valid XML is validated against the XML DTD for its correctness.

XML DTD Structure

The need of DTD ( Document Type Definition) is to ensure that the created XML Structure is Valid and can be parsed. Consider the below XML Data for the student which includes “student.dtd”. The DTD file defines the structure for each used element ( firstName, lastName, registered) in the XML Document.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "student.dtd">
<student>
    <firstName>Mohit</firstName>
    <lastName>Sharma</lastName>
  	<registered>Yes<registered>
</student>

Another common example of notification

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "notification.dtd">
<notification>
 <to>Mohit Sharma</to>
 <from>oracleappshelpadmin</from>
 <subject>New Tutorial on XML DTD </subject>
 <message>We have published the new turotial on XML DTD, XML Document Validation Process</message>
</notification>

XML DTD Structure Definition

The below Student DTD is interpreted as per the below provided definition.

#PCDATA” stands for Parsed Character Data which means that the data element is parsable by XML parser

!DOCTYPE student – depicts the root element of the document which is “student”
!ELEMENT student – depicts that the “Student” element should have the elements ( firstName, lastName, registered)
!ELEMENT firstName – depicts that the element to be of type “#PCDATA”
!ELEMENT lastName – depicts that the element to be of type “#PCDATA”
!ELEMENT registered – depicts that the element to be of type “#PCDATA”

<!DOCTYPE student
[
<!ELEMENT student (firstName,lastName,registered)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT registered (#PCDATA)>
]>	

We can also provide DTD within the XML Document

<?xml version="1.0" encoding="UTF-8"?>
<!-- XML DTD declaration  -->
<!DOCTYPE student
[
<!ELEMENT student (firstName,lastName,registered)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT registered (#PCDATA)>
]>	
<!-- XML DTD declaration  -->
<student>
    <firstName>Mohit</firstName>
    <lastName>Sharma</lastName>
  	<registered>Yes<registered>
</student>