Document Type Definition in XML

What is Document Type Definition -: A Document Type Definition or (DTD) is a set of rules We define in our XML documents. We are using XML to exchange financial information between your bank and your home computer. If the format of your XML document isn’t consistent throughout, your savings account data could be interpreted as your checking account data, causing all kinds of confusion.

DTD in xml

DTD enables us to define the components of an XML document and put them where we want them to be entities, or common elements for XML documents. DTD provide the solution here

  1. Define all elements and attributes of an XML document.
  2. It define the order in which elements and attributes can occur.
  3. It define all entities that can be used in an XML document.
  4. Define the document type

How to declare DTD in our XML documents

DTD are stored in our XML document or in a separate file. If our DTD is going to be located inside in our XML file it must be at the top of our file .The beginning of an internal DTD looks like this type

<!DOCTYPE mydoctype {
........
.......
.......
]>

mydoctype is the document type. If our XML document will hold personal banking information, our doctype might be financial or banking.

System is used if our DTD is a separate file from our XML document. We can define our DTD to be either public or system. It is depending on our application. Public means that all are free to use the DTD. This may be the case if our DTD is defining the XML standard for financial exchange between banks. If we are using our own DTD we will use system.

Types of Declaration of DTD-: There are two way of declaration of DTD in xml

  • Internal
  • External or Neither

Internal Declaration in DTD -: When DTD declaration is internal its called internal declared data type definition.For understand we take a example we can see that there is an XML document that has a definition. The bold part in the following example is the DTD declaration.

<?xml version="1.0">
<!-- XML declare starts here -->
<!DOCTYPE xmltoic [
<!ELEMENT xmltopic (to,from,subject,message))>
<!Element to (#PCDATA )>
 <!Element to (#PCDATA)> 
 <!Element to (#PCDATA)> 
 <!Element to (#PCDATA)> 

 <!-- XML declare Ends here here --> 
<xmltopic>
   <to> My reader </to>
    <from>Netnic</from>
    <subject> Welcome in programming language</subject>
   <message>welcome in http://www.netnic.org </message>
  </xmltopic>

External declaration in DTD-: The external DTD declaration in an XML document, we must include the reference to the DTD file in the definition, as we have done in the following example.

 <?xml version="1.0"> 
 <!DOCTYPE xmltoic  SYSTEM "cc.dtd">
 <xmltopic>
   <to> My reader </to>
    <from>Netnic</from>
    <subject> Welcome in programming language</subject>
   <message>welcome in http://www.netnic.org </message>
  </xmltopic> 

The definition in the above document contains the reference to “cc.dtd” file. Here is the content of “cc.dtd” file that contains the DTD for above XML document.

<!ELEMENT xmltopic (to,from,subject,message))>
<!Element to (#PCDATA )>
 <!Element to (#PCDATA)> 
 <!Element to (#PCDATA)> 
 <!Element to (#PCDATA)>  

Declare Our Entity In Our DTD -: Sometimes we are using the same text over and over again in our documents. we want to include our name, company name, or a copyright notice in every document. In this condition we can’t cut and paste these text over and over. In this problem solution we declare our entity in our DTD.

To use your declared entity, include the entity reference preceded by an ampersand (&) and followed by a semi-colon. For example

<company>&comapny;</company>

There are five entities predefined in XML. All XML processors are required to support these entities.

  1. lt-: It represent less then (<)
  2. gt-: It represent grater then (>)
  3. amp-: It represent ampersand then (&)
  4. apos-:The single quote or apostrophe (‘)
  5. quote-:The double quote (“)

Any And Empty -: In the some condition we will find that our element should be empty. Other times we want may not care if elements are contained within other elements .For this condition we can define these elements in our DTD.

ANY means that any other element can occur in between and EMPTY can be used to denote when an element must be empty.

 <?xml version="1.0">  

 <!Element   phonebook any>

 <!Element   marked empty>

Contend Model-: A content model provides the framework for data to flow into, as it is entered into our XML document, thus keeping the data organized and correctly formatted. Suppose If we are creating an XML document to store contact information for our friends. we want to include both home and work addresses as part of this information. In this condition we can use the contend model.

Leave a Comment