Introduction to DTD (Document Type Definition)by Faisal Khan.
Introduction
In this tutorial, we will learn about "Document Type Definition". DTD is an XML schema language that is used to describe the structure of an XML document. It describes what elements an XML document can contain and in which order. It further specifies how these elements are arranged within one another and what attributes these elements can contain. The description of the structure and these constraints are written in a formal syntax, some of which we will learn in this tutorial. These declarations can be placed within an XML file, or more commonly in a separate file with ".dtd" extension.
Following topics will be covered in this tutorial:
This tutorial is 5th one in the series of tutorials about XML and ASP.NET:
We will now learn how elements and attributes are declared in DTD, and how DTD declarations can be associated with XML document(s).
Declaring Elements in DTD
An element declaration is used in DTD to describe what content, child element(s), and attribute(s), that element can contain. One element declaration is used for each element within an XML document, separately. We begin with the declaration of the root element in our XML file.
<!ELEMENT article (title, author, body)>
Above DTD element declaration states that there must be a root element with the name of "article" and that element must have child elements with the names of "title", "author" and "body", in that order.
Each element that has been referenced by name in the above declaration, now must have its own separate declaration.
<!ELEMENT article (title, author, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Above element declarations that follow the root element declaration ("article") state that these elements can contain character data.
If you want to have the option where you do not put "author" element within an XML document, all you have to do is to put a '?' (question mark) in front of the "author" reference like this:
<!ELEMENT article (title, author?, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT body (#PCDATA)>
A '?' (question mark) in front of an element reference means that, that element can be present zero or one time. Now, an XML document without any "author" element (but with article, title and body elements in proper order) can still be a validated successfully against this DTD.
We will learn about the full list of specifiers that can be used in element declarations in future tutorials.
Declaraing Attributes in DTD
An attribute declaration is used in DTD to describe what attribute(s) a given element can have, what content those attributes should have, and whether these attribute(s) are required, implied (not required), or fixed. To explain the syntax of attribute declaration, we will add an attribute with the name of "email" to our "author" element, that we declared above. Here is how we do it:
<!ATTLIST author email CDATA #REQUIRED>
Above attribute declaration states that the "author" element should have an attribute with the name of "email" and that attribute's value should be character data (CDATA). If we wanted to give the option to our XML writer that this attribute can be omitted, we should have used #IMPLIED instead of #REQUIRED in the attribute declaration statement.
Our DTD declarations now look like this:
<!ELEMENT article (title, author?, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ATTLIST author email CDATA #REQUIRED>
Associating DTD with XML Documents
There are two methods of associating DTD declarations with XML document(s). We will look into both of them.
Introduction
In this tutorial, we will learn about "Document Type Definition". DTD is an XML schema language that is used to describe the structure of an XML document. It describes what elements an XML document can contain and in which order. It further specifies how these elements are arranged within one another and what attributes these elements can contain. The description of the structure and these constraints are written in a formal syntax, some of which we will learn in this tutorial. These declarations can be placed within an XML file, or more commonly in a separate file with ".dtd" extension.
Following topics will be covered in this tutorial:
This tutorial is 5th one in the series of tutorials about XML and ASP.NET:
We will now learn how elements and attributes are declared in DTD, and how DTD declarations can be associated with XML document(s).
Declaring Elements in DTD
An element declaration is used in DTD to describe what content, child element(s), and attribute(s), that element can contain. One element declaration is used for each element within an XML document, separately. We begin with the declaration of the root element in our XML file.
<!ELEMENT article (title, author, body)>
Above DTD element declaration states that there must be a root element with the name of "article" and that element must have child elements with the names of "title", "author" and "body", in that order.
Each element that has been referenced by name in the above declaration, now must have its own separate declaration.
<!ELEMENT article (title, author, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT body (#PCDATA)>
Above element declarations that follow the root element declaration ("article") state that these elements can contain character data.
If you want to have the option where you do not put "author" element within an XML document, all you have to do is to put a '?' (question mark) in front of the "author" reference like this:
<!ELEMENT article (title, author?, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT body (#PCDATA)>
A '?' (question mark) in front of an element reference means that, that element can be present zero or one time. Now, an XML document without any "author" element (but with article, title and body elements in proper order) can still be a validated successfully against this DTD.
We will learn about the full list of specifiers that can be used in element declarations in future tutorials.
Declaraing Attributes in DTD
An attribute declaration is used in DTD to describe what attribute(s) a given element can have, what content those attributes should have, and whether these attribute(s) are required, implied (not required), or fixed. To explain the syntax of attribute declaration, we will add an attribute with the name of "email" to our "author" element, that we declared above. Here is how we do it:
<!ATTLIST author email CDATA #REQUIRED>
Above attribute declaration states that the "author" element should have an attribute with the name of "email" and that attribute's value should be character data (CDATA). If we wanted to give the option to our XML writer that this attribute can be omitted, we should have used #IMPLIED instead of #REQUIRED in the attribute declaration statement.
Our DTD declarations now look like this:
<!ELEMENT article (title, author?, body)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ATTLIST author email CDATA #REQUIRED>
Associating DTD with XML Documents
There are two methods of associating DTD declarations with XML document(s). We will look into both of them.
|