Compare the Difference Between Similar Terms

Difference Between DTD and XSD

Key Difference – DTD vs XSD
 

DTD and XSD are related to XML, which is used for data transfer among its several functions. Most applications require data transfer. When the client machine requires getting data from the server, there should be an effective method to fetch data. XML can be used to achieve this task. It stands for Extensible Markup Language. The main advantage of using XML is to transfer data. Other than that, it can also be used to configure frameworks and for designing the layout for android applications. The tags used in XML are not predefined. The programmer can write the tags according to the application. An XML document contains the structure and the content. In XML, the DTD stands for Document Type Definition and XSD stands for XML Schema Definition. DTD is a set of markup declarations that define a document type for an SGML – family markup language. XSD specifies how to describe the elements in an Extensible Markup Language document formally. The key difference between DTD and XSD is that DTD can be used to define the structure while XSD can be used to define the structure and content.  This article discusses the difference between DTD and XSD.

CONTENTS

1. Overview and Key Difference
2. What is DTD
3. What is XSD
4. Similarities Between DTD and XSD
5. Side by Side Comparison – DTD vs XSD in Tabular Form
6. Summary

What is DTD?

DTD stands for Document Type Definition. It is used to describe XML language precisely. The main objective of DTD is to define the structure of an XML file. It contains a list of legal elements. It is also used to perform validation. There are two types of DTD. They are internal or external. If the DTD elements are declared within the XML file, it is known as internal DTD. If the DTD elements are declared in some other file, it is known as external DTD.

Figure 01: Internal DTD

According to the above, the elements are declared within the XML file. So, it is an internal DTD. The <! DOCTYPE Student defines that the root element of the document is Student. The <! Element Student (ID, Name, Email) > defines that the Student element consists of three elements that are ID, Name and Email. Each ID, Name and Email are defined separately. They all are parse-able data types. DTD exist from line number 2 to 7. The rest is XML.

Figure 02: Adding the external DTD file

When the DTD file is saved as student.dtd, it should be added to the XML file. It is done as follows;

<! DOCTYPE Student SYSTEM “student.dtd”>

What is XSD?

XSD stands for XML Schema Definition. It is used to define the structure and the content of XML files. It is a method of expressing constraint of the XML file. XSD is similar to DTD, but it is providing more control on XML structure. There are two types of XSD files. They are simple Type and complex Type. The simpleType allows having text-based elements. It contains fewer attributes, child elements and cannot be left empty. The complexType allows holding multiple attributes and elements. It contains additional sub-elements and can be left empty.

Figure 03: new1.xsd file

According to the above XSD file, <xs:element name=”Student”> defines that the element name is Student. The <xs:complexType> defines that the element Student is complex type. The <xs:sequence> defines that the complex type is a sequence of elements. The <xs:element name=”ID” type=”xs:string/> defines that the element ID is of string or text type. The name and Email are also of type string or text.

Figure 04: Student.xml file

Above is the XML file, the location of the new1.xsd file should be included inside xsi:schemaLocation.

What is the Similarity Between DTD and XSD?

What is the Difference Between DTD and XSD?

DTD vs XSD

DTD is a set of markup declarations that define a document type for an SGML – family markup language. XSD specifies how to describe the elements in an Extensible Markup Language document formally.
 Stands For
DTD stands for Document Type Definition. XSD stands for XML Schema Definition.
Control on XML Structure
DTD provides less control over the XML structure. XSD provides more control over the XML structure.
 Support for Data Types
DTD does not support data types. XSD supports data types.
Simplicity
DTD is harder than XSD. XSD is simple than DTD.

Summary – DTD vs XSD

XML is a technology for data transferring. DTD and XSD are related to XML. An XML file contains the structure and the content. DTD is a set of markup declarations that define a document type for an SGML – family markup language. XSD specifies how to describe the elements in an Extensible Markup Language document formally. The difference between DTD and XSD is that DTD can be used to define the structure while XSD can be used to define the structure and content.

Reference:

1.“XML Schema (W3C).” Wikipedia, Wikimedia Foundation, 17 Mar. 2018. Available here  
2.“XML DTD – Javatpoint.”, Java T Point. Available here
3.“XML Schema – Javatpoint.”, Java T Point. Available here
4.“Document Type Definition.” Wikipedia, Wikimedia Foundation, 14 Mar. 2018. Available here