How to Create a Document Type Definition (DTD)

An XML file can have a document type definition or DTD which contains and defines tags which can be used in the XML document. DTD is actually antecedent to XML schema which is generally used now as a way to define elements and attributes which can be used to create an XML document.

Creating a DTD means creating elements, attributes and notations for an XML file which contains reference to the DTD and these elements and attributes will define and control the structure of the DTD.

Firstly, we will discuss the DTD elements. Consider the syntax for element declaration as described below. It is must to write element name and content type here. For example, a student element would be defined as:

<! DOCTYPE student [
<! ELEMENT studentname (#PCDATA)>
]>
Here content type is taken as #PCDATA which means parsed character data. The interesting thing is that elements can be empty or can have content. Also elements can have children. For understanding this, consider the following case:

<! DOCTYPE letter [
<! ELEMENT letter (header,message,footer)>
<!ELEMENT header(#PCDATA)>
<!ELEMENT message(#PCDATA)>
<!ELEMENT footer (#PCDATA)>
]>
Now we will move to the DTD attributes. Following is the declaration syntax for attribute ‘subject’ of element ‘student’ :

<! ATTLIST student subject CDATA “English”>

It is clear that apart from attribute name, an attribute can have attribute type and default value. Here type is CDATA which implies that value is character data and its default value is English. An attribute can also be defined as required, implied(if its value is not necessary) or fixed. As clear from the name it is compulsory for a attribute to have value if it is declared as required.

In addition to elements and attributes, DTD can have external or internal entities which are variables used to place some special characters. The following syntax is used to declare entities:

<! ENTITY entityname “entityvalue”>
For example,
<! ENTITY studentname “richa”>
The above example is of internal entity declaration. DTD can also be declared externally by using following syntax:
<! ENTITY entityname SYSTEM “url”>
For example,
<!ENTITY product SYSTEM “http://www.productdescription.com/product.dtd”>

Now we have gone through the important component of a DTD which further help in structuring an XML document. We can declare the DTD (which would be used in defining the XML document ) inside the document as well as externally.

Also it is possible to validate an XML document against its corresponding DTD. This is done by the use of “validateOnParse” property (of Document object which represents an XML document). It its value is set “true”, it tells XML parser
to check the document against its DTD. Its default value is true.

| About Runtime Components of .NET Framework | About Session Management in a Web Based Enterprise Application | An introduction to Microsoft Mobile Internet Toolkit (MMIT) | How to Internationalize Your .NET Application | How to Create a Document Type Definition (DTD) | How to Create a .NET Windows Service Easily | Knowing the Difference Between Application Server and Web Server | Understanding Key Advantages of .NET Type System | Understanding Some Basic Language Related Features of C# |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.