Document Type Definition (DTD) : Well-Formed
Document Type Definition (DTD) : Well-Formed
Document Type Definition (DTD) : Well-Formed
DTD is used to define the rules and attributes for using the tags in a
XML document.
Well-formed: If the XML document adheres to all the general XML rules
such as tags must be properly nested, opening and closing tags must be
balanced, and empty tags must end with '/>', then it is called as well-
formed.
If a XML document follows the rules given by the DTD then the
document is valid xml document.
A DTD defines the XML document structure with a list of legal elements
and attributes.
A DTD defines the structure of XML document like elements and their
relation, hierarchy, attributes and entities.
The set of rules for a XML document can be specified using DTD Types as
1. Inline or Internal DTD: The structure and rules of XML document are
specified in the XML document.
2. External DTD: The structure and rules of XML document are specified in
a separate file with “.dtd” extension, referenced in XML document.
In external DTD elements are declared outside the XML file. They
are accessed by specifying the system attributes which may be
either the legal .dtd file or a valid URL.
Internal Entity
If an entity is declared within a DTD it is called as internal entity.
<address>
&phone_no;
</address>
External Entity
If an entity is declared outside a DTD it is called as external entity. You
can refer to an external Entity by either using system identifiers or public
identifiers.
<emps>
<emp>
<id prefix="INF">101</id>
<name>Ramesh</name>
<sal>85900</sal>
<dept grade="B"> Developer</dept>
<company><&address;></company>
</emp>
<emp>
<id>121</id>
<name>Suresh</name>
<sal>65900</sal>
<dept grade="C"> Tester</dept>
<company>"&address;"</company>
</emp>
<emp>
<id>131</id>
<name>Ganesh</name>
<sal>185900</sal>
<dept grade="A">Designer</dept>
<company>'&address;'</company>
</emp>
</emps>
<!—Products Info Using Inline or Internal DTD
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE products [
<!ELEMENT products (product+)>
<!ELEMENT product (pid,pname,desc,price,qty)>
<!ELEMENT pid (#PCDATA)>
<!ELEMENT pname (#PCDATA)>
<!ELEMENT desc (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT qty (#PCDATA)>
]>
<products>
<product>
<pid> LG102 </pid>
<pname> LG ULTRA HD 50 </pname>
<desc> High Definition </desc>
<price> 119000 </price>
<qty> 12 </qty>
</product>
<product>
<pid> SS110 </pid>
<pname> SAMSUNG CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 139000 </price>
<qty> 8 </qty>
</product>
<product>
<pid> MI222 </pid>
<pname> MI CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 69000 </price>
<qty> 14 </qty>
</product>
</products>
External DTD Examples
<!—Employees Info Using External DTD
emp.dtd
<!ELEMENT emps (emp+) >
<!ELEMENT emp (id,name,sal,dept,company)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT sal (#PCDATA)>
<!ELEMENT dept (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ATTLIST dept grade CDATA #REQUIRED
block CDATA "A">
<!ATTLIST id prefix CDATA #IMPLIED>
<!ENTITY address "Infosys Technoligies, Gachibowli, Hyderabad">
empexter.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE emps SYSTEM "emp.dtd" >
<emps>
<emp>
<id prefix="INF">101</id>
<name>Ramesh</name>
<sal>85900</sal>
<dept grade="B"> Developer</dept>
<company><&address;></company>
</emp>
<emp>
<id>121</id>
<name>Suresh</name>
<sal>65900</sal>
<dept grade="C"> Tester</dept>
<company>"&address;"</company>
</emp>
<emp>
<id>131</id>
<name>Ganesh</name>
<sal>185900</sal>
<dept grade="A">Designer</dept>
<company>'&address;'</company>
</emp>
</emps>
<!—Products Info Using External DTD
product.dtd
<!ELEMENT products (product+)>
<!ELEMENT product (pid,pname,desc,price,qty)>
<!ELEMENT pid (#PCDATA)>
<!ELEMENT pname (#PCDATA)>
<!ELEMENT desc (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT qty (#PCDATA)>
prodexter.xml
<product>
<pid> SS110 </pid>
<pname> SAMSUNG CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 139000 </price>
<qty> 8 </qty>
</product>
<product>
<pid> MI222 </pid>
<pname> MI CURVE HD 50 </pname>
<desc> High UV Definition </desc>
<price> 69000 </price>
<qty> 14 </qty>
</product>
</products>
<!—MotorBikes Info Using internal DTD
motorbike -> make,model,year,color,engine,chasis-num,accessories.
engine -> eng-no,cylinders,fuel-type.
accessories attributes -> disk-brake => “yes or no” , auto-start =>”yes or no”.
use appropriate entities.
<catalog>
<motorbike>
<make> HeroHonda</make>
<model>PassionPro</model>
<year> 2019</year>
<color>Black</color>
<engine>
<eng-no> 2345EF</eng-no>
<cylinders>2 </cylinders>
<fueltype> Petrol</fueltype>
</engine>
<chasisnum>1122</chasisnum>
<accessories diskbrake="yes" autostart="no"> ALL Reuired , &mirror;</accessories>
</motorbike>
<motorbike>
<make> Honda Active</make>
<model>5G</model>
<year> 2018</year>
<color>RED</color>
<engine>
<eng-no> 12345EF</eng-no>
<cylinders> 4 </cylinders>
<fueltype> Petrol</fueltype>
</engine>
<chasisnum>110022</chasisnum>
<accessories diskbrake="yes" autostart="yes"> ALL Reuired , &mirror;</accessories>
</motorbike>
</catalog>