DTD Introduction

In this tutorial you will learn about DTD Introduction and how to use in XML. DTD (Document Type Definition) is a type of document schema and define the structure of XML documents.

DTD is a provide a framework for validating XML documents. You can create DTD file that are shareable to a different application.

DTD Introduction

In XML you can define tags without defining what tag are legal. But defined XML document structure must be conform to, if you specifies DTD rules.

DTD does not identify root element. Manually you want to inform (write) root element. In sort DTD contains number of rules that rules must be follow XML document.

DTD Introduction - DTD Tutorial


DTD Rules

DTD defines following three rules,

  • Specifies the tags and attributes that can be used to creating XML document.

  • How to tags combines and reuse.

  • Specifies the entities which are represent the special characters.

Well-formed XML

A Well-formated file is follow general XML rules like every open tag must be closed, tags must be properly nested, empty tag must be end with '/>', attribute values must be enclosed either single or double quotes etc.

Valid XML

Valid XML file is conforms to a specific structure and that XML file have DTD that specifies used tags, attributes those are tag contains.

Types of DTD declarations

You can specifies DTD either internally within XML document or externally.

External DTD You can write rules in a separate file (with .dtd extension). later this file linked to a XML document. This way you can linked several XML documents refer same DTD rules.

Internal DTD You can write rules inside XML document using <!DOCTYPE ... > declaration. Scope of this DTD within this document. Advantages is document validated by itself without external reference.

<!ELEMENT PARAM - O EMPTY    -- named property value -->
<!ATTLIST PARAM
  name        CDATA         #REQUIRED   -- property name --
  value       CDATA         #IMPLIED    -- property value --
  valuetype   DATA|REF|OBJECT   DATA    -- type of value that represent how to interpret --
  type        CDATA         #IMPLIED    -- Media type --
>

Comment Example

<!ELEMENT IMG - O EMPTY      --  Embedded to a image -->
<!ATTLIST IMG
  src         %URL       #REQUIRED -- URL Path --
  alt         CDATA      #IMPLIED  -- image description --
  height      %Pixels    #IMPLIED  -- specifies the height --
  width       %Pixels    #IMPLIED  -- specifies the width --
>