Difference between DTD and XSD schema

DTD(Document Type Definition) is a document defining and constraining a set of statements written following the rules of Standard Generalized Markup Language (SGML). HTML is a type of DTD which is compiled locally by the browser in accordance to the tags mentioned in the document.

XSD(XML Schema Definition) is a W3C recommendation which lays down certain rules for describing the elements in an XML document. XML serves two important purposes:

  1. It contains data types, variables and objects which is similar to any other programming language entities.
  2. This document is written in a well-structured manner, which is very easily interpreted.


XSD vs DTD schema
  1. DTD does not support namespaces, it has its own set of keywords for defining schema. To write DTD document, we should know these keywords. ex: !ELEMENT for root tag, !ATTLIST for attribute, !ENTITY for variables.
    XSD is quite easy to learn as it doesn't contain hard-coded keywords and tags, we can build our own namespace.
  2. DTD doesn't support code reusablility. Whereas XSD allows us to include elements from other namespace.
    <xs:schema xmlns="http://www.example.com"> xmlns:xs="http:www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com" <xs:include schemaLocation="include_file.xsd" /> </xs:schema>
    Now we have accessed to the namespace of target location (https:www.example.com).
  3. DTD supports only one type to store data which is String type and is declared in DTD with #PCDATA keyword.
    XSD on the other hand has different datatypes as per usage.
    Primitive or inbuilt datatypes: string, decimal, float, boolean
    User defined datatypes
  4. DTD is weakly typed. "Weakly typed" tells us that we cannot set constraints in it. Since XSD is based on XML, We can define the type of data that an element should be holding, and can even set constraints like length of data.
  5. DTD allows us to use inline definitions. When we're working with a small file, we can write both schema(layout) and content within the same document. XSD doesn't allow such definitions.
Solution Worked 0 UpvotesUpvote
Solution Didn't Worked 0 DownvotesDownvote