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:
- It contains data types, variables and objects which is similar to any other programming language entities.
- This document is written in a well-structured manner, which is very easily interpreted.
DTD Vs. XSD:
-
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.
-
DTD doesn't support code reusablility. Whereas XSD allows us to include elements from other namespace.
xmlns:xs="http:www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com"
Now we have accessed to the namespace of target location (https:www.example.com).
-
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
-
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.
-
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.