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 [ <! DOCTYPE
letter [ <! 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. <! ENTITY
entityname entityvalue> 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
|