DTD in XML

In this article I am going to explain about DTD.
  • 1720

XML DTD

In XML DTD stands for Document type definition. DTD defined structure of XML document and list of element and attribute. A DTD can be defined inline inside an XML document, or as an external reference.

An DTD has fowling rule

  • XML documents must have a root element
  • XML elements must have a closing tag
  • XML tags should be case sensitive
  • XML elements must be properly in the nested
  • XML attribute values must be quoted

Use of  DTD

  • With a DTD our XML file should be carry description in own format.
  • With DTD Independent group of people can be agree to use standard DTD foe interchanging data.
  • Our Application can be use verify that data we use from outside from the world.   
  • We can also use DTD to verify our own data.

Declaration of Internal DTD.

When you declared DTD  inside the XML file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element [element-declarations]>

 Declearation of  XML with Internal DTD.

<?xml version="1.0"?>
<!DOCTYPE Detail [
<!ELEMENT Detail (to,from,heading,body)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Add (#PCDATA)>
<!ELEMENT Company (#PCDATA)>
<!ELEMENT Salary (#PCDATA)>
]>
<Detail>
<Name>prabhakar</Name>
<Add>New Delhi</Add>
<Company>MCN solution</Company>
<Salary>10000</Salary>
</Detail>

Declaration Of External DTD

When you decleared DTD in an external file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element SYSTEM "filename">

Declearation of DTD with External DTD.

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "Detail.dtd">
<Detail>
  <Name>prabhakar</Name>
  <Add>Delhi</Add>
  <Company>MCN solution</Company>
  <Salary>10000</Salary>
</Detail>

Its External file that contain DTD.

<!ELEMENT Detail (Name,Add,Company,Salary)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Add (#PCDATA)>
<!ELEMENT Company (#PCDATA)>
<!ELEMENT Salary (#PCDATA)>

Further Readings

You may also want to read these related articles: here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

 

© 2019 DotNetHeaven. All rights reserved.