Open In App

DTD Components

Markup Languages play a very important role in encoding and standardizing information that is shared over the web. XML (Extensible Markup Language), being one of those languages, provides a set of rules to encode data in the document which is both human as well as machine-readable.

For this, XML uses a unique specification norm to define the structure of a document which is called a Document Type Definition (DTD). In this article, we have covered every minute detail about DTD and its components. So, without any delay, let us delve deep into the DTD Components.



Overview of DTD

Document Type Definition(DTD) includes the formal specification that describes the structure, legal elements, and attributes of an XML Document. We can say that it acts like a rulebook that specifies the structure and relationship between the elements of an XML Document. It also checks the validity and vocabulary against the grammatical rules of XML language.



Moreover, it defines the elements, their syntax, and rules to use them in an XML document. Hence, a valid and well-formed XML document must conform to the DTD specifications. Below is the syntax of DTD in a ‘sample.dtd’ file:

<!DOCTYPE rootElement [
    <!-- DTD rules are defined here →
    Declaration 1…
    Declaration 2…
]> 

Where the DOCTYPE is a delimiter, rootElement is the starting element parsed by the parser, and the square brackets define the list of declarations.

What are DTD Components?

DTD components are the building blocks of the XML Document which are described by the DTD. These are nothing but the XML Components that are described in DTD in terms of syntax, validation, and their order in XML files. The various DTD Components are described below:

Element:

This component describes notations to define an element, its required components, and any elements it can or cannot contain. Its syntax is outlined below:

<ELEMENT element_type minimization (content model) >

Where the element_type is the element name or tag name, for example, <head> tag. The ‘minimization’ denotes a two-character entry that indicates whether a start or an end tag is required or not. And, the ‘content model’ describes the list, sequence, and occurrence of the elements.

Attribute:

An attribute lists the additional properties that describe an element. The syntax of the Attribute component is shown below:

<!ATTLIST element-name attribute-name attribute-type attribute-value>

Here, ATTLIST denotes the attribute element, ‘element-name’ defines the name of the element, and ‘attribute-name’ defines the name of the attribute. And, ‘attribute-type’ and ‘attribute-value’ tells the type and value of the attribute.

Entities:

DTD Entities define the alternatives to certain characters in an XML document. It tells value to be in place of some special characters in a document. They can be defined internally and externally. The syntax of Entities is mentioned below:

<!-- Internal Entity -->
<!ENTITY entity_name "entity_value">
<!-- External Entity -->
<!ENTITY entity_name SYSTEM "URI/URL">

Here, ‘entity_name’ and ‘entity_value’ are the name and replacement values for the entity

In the Internal Entities, values are specified within the DTD tag. While in the external Entity, they are specified outside the DTD component.

Now, we will see some examples with the syntax for the sake of better understanding.

Examples of DTD Components

Example of DTD Element:

Suppose you specify the information about the library in an XML file as follows:

<!-- books.xml -->
<!DOCTYPE library SYSTEM "library.dtd">
<library>
   <book>
      <title>Harry Potter and the Sorcerer's Stone</title>
      <author>J.K. Rowling</author>
      <price>19.99</price>
   </book>
   <book>
      <title>The Great Gatsby</title>
      <author>F. Scott Fitzgerald</author>
      <price>14.95</price>
   </book>
</library>

Thus, to describe the validated syntax, elements, and attributes, the DTD code for Element component is shown below:

<!-- library.dtd -->
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)> <!-- PCDATA means parsed character data of element-->
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>

Here, the plus(+) sign signifies that the element can appear more than once in the document.

Example of DTD Attribute:

Imagine that you have to define attributes for Inventory Management data using the XML file as shown below:

<!-- products.xml -->
<!DOCTYPE inventory SYSTEM "inventory.dtd">
<inventory>
   <product id="101">
      <name>Laptop</name>
      <price>899.99</price>
   </product>
   <product id="102">
      <name>Smartphone</name>
      <price>499.99</price>
   </product>
</inventory>


Therefore, to list the attributes, the DTD code is as outlined below:

<!-- inventory.dtd -->
<!ELEMENT inventory (product+)>
<!ELEMENT product (name, price)>
<!ATTLIST product id CDATA #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT price (#PCDATA)>



Here, the ATTLIST defines the attribute and CDATA means the Character data of the attribute to be parsed.

Example of DTD Entity:

If you want to specify the replacement value for an entity named ‘company’, the XML code will be:

<!-- company.xml -->
<!DOCTYPE organization SYSTEM "organization.dtd">
<organization>
   <name>&company;</name>
   <department>IT</department>
</organization>


Now, the DTD code to define the replacement for the entity would be:

<!-- organization.dtd -->
<!ENTITY company "XYZ Pvt Ltd ">
<!ELEMENT organization (name, department)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT department (#PCDATA)>


It means that whenever the ‘company’ name is used, the information about the company defined in the DTD code will be referenced.

After learning about the DTF components, let us see their notable features.

Features of DTD Components

Advantages of DTD Components

Disadvantages of DTD Components

Conclusion

DTD Components describes the building blocks of the XML document and the rules to validate them as per the standards. Each component in DTD has some utility in the XML documentation, to describe the attributes and entities, whether they are mandatory or optional, their order, and their occurrences in the document.


Article Tags :