What are the differences between DTD and XSD?

XMLDynamic ProgrammingProgramming

Let us understand the concepts of XML Schema Definition (XSD) and Document Type Definition (DTD) before learning the differences between them.

XML Schema Definition (XSD)

XML is called an Extensible markup language which is used for the representation and manipulation of the data elements. It is a language which is used to communicate the data in the form of structures on the internet.

XSD is called as XML scheme definition is the more extended version of the data definition language and is used to explain the structure of the XML schema. The XML features are that it explains the document in a more precise way that is understandable to the users.

Uses of XML Schema

The uses of XML Schema are as follows −

  • XSD provides the structure and checks that XML file is created according to structure or not and they help programmers to make or enter wrong details in a file.

  • When the data is sent over the internet that is from the sender to receiver it is important to use XML schema because it secures the data.

  • By using XML schema it is very easy to connect with the database and to explain the representation patterns that are data formats.

  • It is also useful to convert the data from one format to the other format.

Example

Consider a file names as page.xml as −

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
   <xs:complexType>
      <xs:sequence>
         <xs:element name="to" type="xs:string"/>
         <xs:element name="from" type="xs:string"/>
         <xs:element name="heading" type="xs:string"/>
         <xs:element name="body" type="xs:string"/>
      </xs:sequence>
   </xs:complexType>
</xs:element>
</xs:schema>

The XSD file is saved with the .xsd extension which provides the structure to an XML file or document. The XSD file for the above xml code is as −

The above example of XSD file names as page.xsd and that file is used in the xml document. The keywords ‘from’,’heading’ and ‘body’ contain the type of strings and for use sequentially we put them in between sequence tags and close the schema.

Document Type Definition (DTD)

The Document type definition is also called as DTD helps us to provide the logical methods and syntax for an XML document. The extension used for DTD is .dtd gives the full explanation about the constraints that are formats, elements of XML documents.

It is a markup declaration that supports markup languages. The presentation of the DTD document as −

https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd

To use the DTD in XML format, the programmer has to send the above declaration in an XML document.

DTD provides the declaration such as how to create root and their child elements and how they relate to each other.

Syntax

The syntax for DTD is as follows −

<!DOCTYPE element DTD identifier
[
   declaration1
   declaration2
   ........
]>

Uses of DTD

The uses of DTD are as follows −

  • DTD is used for providing the syntax and logical structure to an xml document

  • It makes the code readable and more understandable for the programmer and stores the data in XML very well formed without complexity.

Example

Given below is the representation of DTD −

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE address [
   <!ELEMENT address (name,company,phone)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT company (#PCDATA)>
   <!ELEMENT phone (#PCDATA)>
]>

<address>
   <name>Tanmay Patil</name>
   <company>TutorialsPoint</company>
   <phone>(011) 123-4567</phone>
</address>
raja
Updated on 17-Mar-2022 10:56:57

Advertisements