Open In App

What are the Differences between Internal DTD and External DTD ?

Last Updated : 09 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

DTD stands for Document Type Definition, and it is used to define the structure and content of an XML document. An  XML document can have an internal DTD or an external DTD, depending on the needs of the user. In this article, we will discuss the differences between internal and external DTDs. This article will discuss the dissimilarities between these two types of DTD, including their syntax, sample usage, and variations presented in tabular form.

Internal DTD: Internal DTD is a type of Document Type Definition (DTD) in  XML that is written within the  XML document itself. It specifies the structure and rules for the elements and attributes of the XML document. An internal DTD is enclosed within the <!DOCTYPE> declaration of the XML document and is defined using a set of predefined keywords and syntax. Internal DTDs are suitable for smaller  XML documents where the complexity of the structure is not very high. It is easier to maintain and modify the internal DTD as it is part of the XML document itself.

Syntax:

<!DOCTYPE root_element[ <!ELEMENT element_name (element_content)>
<!ELEMENT another_element_name (another_element_content)>
]>

Example: In this example, we will show the internal DTD. 

XML




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE customers[ <!ELEMENT customers (customer+)>
    <!ELEMENT customer (name, email, phone)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT email (#PCDATA)>
    <!ELEMENT phone (#PCDATA)>
]>
    <customers>
        <customer>
            <name>Satyam Nayak</name>
            <email>Satyam@Nayak.com</email>
            <phone>112-123-1234</phone>
        </customer>
        <customer>
            <name>Sonu N</name>
            <email>Sonu@N.com</email>
            <phone>112-455-9969</phone>
        </customer>
    </customers>


Output:

Internal DTD

The DTD defines the structure of an XML document that contains customer information. The XML document contains two customer elements, and each customer element contains a name, email, and phone element.

External DTD: External DTD is a type of Document Type Definition (DTD) in XML that is located outside of the actual XML document it describes. It can be stored in a separate file or accessed via a URL, and it defines the structure, rules, and constraints for the elements and attributes within an XML document. By using an external DTD, multiple XML documents can share the same set of rules and constraints, leading to more consistency and easier maintenance. External DTD can also be updated independently without having to modify the XML documents themselves.

Syntax:

<!DOCTYPE root_element SYSTEM "DTD_file_name">

Example: In this example, we will show the external DTD

XML




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE customers SYSTEM "customers.dtd">
<customers>
    <customer>
        <name>Satyam Nayak</name>
        <email>Satyam@nayak.com</email>
        <phone>122-112-1234</phone>
    </customer>
    <customer>
        <name>Sonu N</name>
        <email>Sonu@N.com</email>
        <phone>112-554-9969</phone>
    </customer>
</customers>


Output:

External DTD

The DTD is defined in a separate file called “customers.dtd.” The XML document references the DTD using the DOCTYPE declaration. The DTD defines the structure of an XML document that contains customer information.

Differences between internal DTD and external DTD:-

Feature Internal DTD External DTD
Syntax Embedded  Linked
File location  Within XML  Separate file
Document size  Larger  Smaller
Document sharing  Difficult  Easy
Document update  Difficult use Easy
Document parsing Slightly slower Slightly faster
Security and validation More secure and reliable Less secure and reliable

XML documents can contain a DTD, which can either be embedded within the document itself (known as an internal DTD) or stored in a separate file (an external DTD). Internal DTDs can result in larger XML documents, while external DTDs keep them smaller. External DTDs can be shared and reused among multiple XML documents more easily than internal DTDs because they are stored separately. Updating external DTDs is also more efficient because changes made to the DTD file will automatically apply to all linked documents. However, updating internal DTDs would require updating each XML document individually.

In conclusion, both internal and external DTDs have their advantages and disadvantages, and the choice between the two depends on the needs of the user. Internal DTDs are useful for small XML documents with simple DTDs, while external DTDs are useful for large XML documents with complex DTDs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads