XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

XML DTD

XML DTD

shape Introduction

The modules till now have covered well-formed XML documents that are unconstrained in the content that they contain. XML syntax is distinguished from other expressions of information by incorporating the separate but related concepts of constraints and validity. One can express constraints on the elements and attributes and their values and one can then validate an XML document against those constraints. Even without validity being considered, declared constraints on attributes can be treated as characterizations that impact on the information delivered from an XML processor to an application. The Document Type Definition, known better by the acronym DTD, is not the same as the document type declaration, which is just the markup construct that contains markup declarations that include, among other things, the declarations of the DTD. Whereas, the DTD is the XML specification's way to define a document model. It has an arcane syntax that is inherited from SGML.

shape Description

A DTD can be defined as a set of markup declarations that describe the XML vocabulary which is the specification of elements and attributes. The DTD or document type definition is an optional part of an XML file. This model specification has two primary purposes. If an XML file has a DTD, it is read along with the document entity and the name of document type definition emphasizes its use in defining the type of the document. As a type definition, it works a bit differently than types used in common programming languages. The DTD is not typically used to create an instance of a type but it can create. Instead, it is mainly used to validate an XML document i.e. check to see if the XML document is an instance of the type the DTD specifies. A DTD type definition focuses on the logical structure of an XML document, not the information that it might represent. Because of this, DTD's are limited for defining the structure of XML documents intended to represent data.

DTD Format

shape Syntax

The basic syntax of DTD will be as follows: [xml] <!DOCTYPE element DTD identifier [ declaration1 declaration2 ........ ]> [/xml] In the above syntax,
  • <!DOCTYPE : The DTD always starts with <!DOCTYPE which is always in upper case letters.
  • element: It indicates name of the root element. XML document entity always has the root element that is just a single element that has no parent or other enclosing element.
  • DTD identifier: It defines the path of a file on the system or URL to a file on the web.

DTD Declarations

shape Description

A DTD is a set of markup declarations, broken up into two subsets. Even thought the XML recommendation uses the term subset in some case the order of declarations is important unlike a typical mathematical set.

Internal DTD Subset

If the elements are declared inside the XML files then it can be referred as Internal DTD. In order to refer a file as internal DTD, the attribute standalone in XML declaration should be set to yes. the internal subset of markup declarations is enclosed in square brackets [ ]. Syntax: [xml] <!DOCTYPE root-element [element-declarations]> [/xml] Example: Below code is an example of Internal DTD Subset in which the attribute standalone is set to "Yes" by declaring that DTD is declared inside the XML itself. [xml] <?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>Andy Murray</name> <company>SPLessons</company> <phone>(011) 123-4567</phone> </address> [/xml]

External DTD Subset

If the elements are declared outside the XML files then it can be referred as External DTD. The external subset is identified by a system or public identifier which may be either the legal .dtd file or a valid URL. In order to refer a file as external DTD, the attribute standalone in XML declaration should be set to no. Syntax: [xml] <!DOCTYPE root-element SYSTEM "file-name"> [/xml] In the above syntax, file name should be saved with .dtd extension. Example: Below code is an example of External DTD Subset in which the attribute standalone is set to "no" by declaring that DTD is declared outside of the XML document and is retrieved with the help of SYSTEM Identifier. [xml] <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <!DOCTYPE address SYSTEM "contact.dtd"> <contact> <name>Andy Murray</name> <company>SPLessons</company> <phone>(011) 123-4567</phone> </contact> [/xml] The file contact.dtd will have the below code. [xml] <!ELEMENT contact (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)> [/xml]

Summary

shape Key Points

  • A DTD can be defined as a set of markup declarations that describe the XML vocabulary which is the specification of elements and attributes.
  • Internal DTD and External DTD are the types of DTD.