Difference between DTD vs XSD
DTD stands for Document Type Definition and XSD stands for XML Schema Definition are meant for data transfer which is related to functions of XML data models with reputed industry standards and is considered to be a building block of XML. Both the schema defines the schema structure of an XML Document. Document Type Definition (DTD) can be defined as a legal standard schema definition with the inbuilt specification which helps in defining a document type for the SGML family. DTD being an older one provides metadata which is a simple grammar for defining the XML Document and declared as DOCTYPE next to the XML version. While XML Schema, improved version of DTD benefits by giving details like what the data should have and not.XML Schema refers XML Namespaces and XML DTD for reference and defining. In this topic, we are going to learn about DTD vs XSD.
Head to Head Comparison Between DTD vs XSD (Infographics)
Below are the top differences between DTD and XSD:
Key differences of DTD vs XSD
Here are the following key differences mentioned below:
- Here no defined Data Types are supported. DTD has a very limited amount of operations.
- To associate itself with XML documents DTD uses DOCTYPE.
- Schema is well defined in DTD by their own set of Keywords.
- DTD has both inline and external definitions while XML schema lacks.
- DTD is well suited for small XML documents like companies, school databases.
The below example of DTD is external DTD. When an element holds a child it is specified using commas as in the line 1 Statement.
<?xml version="1.0"?> <!DOCTYPE Library SYSTEM "new.dtd"> <library> <bookname>Introduction to C++</bookname> <authorname>Bala Gurusamy</authorname> <email>firstname.lastname@example.org</email> </library> new.dtd <!ELEMENT library (bookname,authorname,email)> <!ELEMENT firstname (#PCDATA)> <!ELEMENT authorname (#PCDATA)> <!ELEMENT email (#PCDATA)>
- Here XSD Supports Numeric, String, and Boolean Data Types and also internet data types Like ISO, language codes.
- XSD allows us to use namespaces for well-defined Schemas. Improvisation of DTD operations is performed by XSD. to avoid name conflicts in the elements.
- To Associate itself with XML Document Schema performs some specifications like.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema name.xs"
- It takes the features of Object-Oriented Programming Like Inheritance and encapsulation concepts to format the XML document. And XML schema doesn’t allow inline Definitions.
- XSD is more preferred in large XML data like Most popular Web services.
- Elements play a vital role in the XML document and therefore they are defined in XML Schema like
<xs: element name ="name" type ="d"/> here name=Name appeared in XML document and type denotes description part. Example for a sample XML document like <customer> fromUSA </customer> The xsd is given as <xs: element name ="customer " type="xs:string"/>
- Here in XSD, you can specify cardinality like minOccurs and maxOccurs attributes and their default value is ‘1’.
<?xml version="1.0"?> <Airline_db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eee.xs"> <Aerostar Employee_id="10"> <Name>Johanas john</Name> <Type_flight>American Airline</Type_flight> <Contact>02-1512-3547</Conact> <Email>johanas will@ hotmail.com</Email> </Aerostar> <Aerostar Employee_id="18"> <Name>Tom brutt</Name> <Type_flight>Air India</Type_flight> <Contact>03-1622-9722</Conact> <Email>astius_87@ hotmail.com</Email> </Aerostar> </Airline_db> eee.xs <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > <xs:element name="Airline_db" type="AirlineInfoType" /> <xs:complexType name="AirlineInfoType"> <xs:sequence> <xs:element ref="Aerostar" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> <xs:element name="Aerostar" type="AerostarType" /> <xs:complexType name="AerostarType"> <xs:sequence > <xs:element ref="Name" /> <xs:element ref="Type_flight" /> <xs:element ref="Contact" /> <xs:element ref="Email" /> </xs:sequence> <xs:attribute name="Employee_id" type="xs:int" use="required"/> </xs:complexType> <xs:element name="Name" type="xs:string" /> <xs:element name="Type_flight" type="xs:string" /> <xs:element name="Contact" type="xs:string" /> <xs:element name="Email" type="xs:string" /> </xs:schema>
Comparison Table of DTD vs XSD
Now let’s draft the comparison between DTD and XSD in the table below.
|Abbreviation||Abbreviated as Document Type Definition and is not written in XML Language but comes from the SGML family.||Abbreviated as XML Schema Definition, it is written in the XML document.|
|Purpose||It specifies in defining the structure of XML Document||XSD is used to specify both the structure and content.|
|Markup Validation||Specifies only the root element.||Here any element which is made global can be done as root.|
|Data Status||DTD doesn’t have any restrictions on data used.||XSD specifies certain restrictions on data|
|Categories||There are two categories of DTD, they are internal and external. If the DTD declaration is done within the XML file it is termed as internal and outside the XML file is termed as external.||The two types of SXD are Simple and complex types. The simple type has text elements whereas Complex types hold multiple elements and attributes as well.|
|Learning Skill||DTD is not much easy to learn and has less control over XML.||XSD is simple in learning as it sits with the existing system language.XSD provides more control access to the XML structure.|
|Syntax||Unique Syntax is given here||Here XML format is followed by XSD.|
|Element Declaration||ELEMENT keyword is used in DTD||<xs: element name=” element name” type=”xs:datatype” / >|
|Extension||File saved as .dtd||XSD file is saved as .xsd file|
|Repeat count Specification||DTD use (*) for 0 times and (+) for more times||The schema uses min and max occurs attributes and for unlimited it uses unbound.|
|Code-reuse||Code reusability is poor in DTD instead of parameter entities that can be used.||They have good code reusability by using name types.|
|Attribute declaration||ATTLIST keyword is used in DTD Here description is required and implied||XSD uses attribute elements. The description can be omitted optionally. For instance, considering salary attribute, xml schema attribute declaration is given as:
<xs:attribute name=”salary” type=”xs:numeric” use=”optional”/>
|Order of the tree||The order of child elements is not defined here||Here the order of child elements is defined exclusively.|
|Data Types||DTD uses #PCDATA which is a string data type.||It uses fundamental and primitive data types.|
|Typing Status/ Advantage||DTD is not a strongly typed mechanism, which means it is weakly typed with no validating the content.||Here it is Strongly typed as the data declared here is accurate.|
Care must be taken when dealing with schemas as they become too long, Since the DTD syntax is more compact, they tend to be shorter. And also depends upon the tools which we use every day you can either stick into DTD or Schema.
To conclude, XML is a wide technology for transferring data and these DTD and XSD are related to an XML. DTD being a mark-up declaration defines document type for SGML whereas XSD explains how to describe the elements in XML. Here we have seen the comparison between those two with an example. Finally, schemas are more preferable and richer when compared to DTD in the case of defining the XML Document. I hope that all XML professionals develop a quiet little grasp from this article.
This is a guide to DTD vs XSD. Here we discuss the DTD vs XSD key differences with infographics and comparison table. You may also have a look at the following articles to learn more –