Xml DTD Tutorial | What Is DTD

a dtd specifies a set of rules for the structure of a document.

a dtd is associated with an xml document by means of a document type declaration. the dtd(document type declaration) appears in the syntactic fragment doctypedecl near the start of an xml document.

a document type definition lists the elements, attributes, entities, and notations that can be used in a document,as well as their possible relationships to one another.a document type declaration begins with <!doctype    and ends with a >.in between is the name of the root element.a document type declaration has this basic form:-

          root_element system 
          "url of the external dtd subset"
          [internal dtd subset]  >

note :- whitespace is not allowed to appear in between doctype and the opening <!

dtds make two sorts of declaration:

  • an optional external subset
  • an optional internal subset

dtd limitations dtd are a fundamental part of the xml recommendation.dtd will continue to be used in many diverse situations, even as other methods of describing emerge.it began to reveal some limitations of dtds :-

  • differences between dtd syntax and xml syntax.
  • poor support for xml namespaces.
  • poor data typing.
  • limited content model descriptions.

internal dtd declaration

the declarations in the internal subset form part of the document type declaration in the document itself.

the internal dtd syntax.

<!doctype   root-element    [element-declarations-parts]>

a complete xml document with an internal dtd:

       <?xml  version="1.0"?>
             <!doctype         book    [ 
             <!element         book    (author,price,pages,type,code) >
             <!element         author  (#pcdata) >
             <!element         price   (#pcdata) > 
             <!element         pages   (#pcdata) >
             <!element         type    (#pcdata) >
             <!element         code    (#pcdata) > ]>

some quick points about this example

  • !doctype book use to defines the root element of this document is book.
  • !element book use to defines the book element contains four elements    (author,price,pages,type,code).
  • !element author use to defines the author element to be of type '#pcdata'
  • !element price use to defines the price element to be of type '#pcdata'
  • !element pages use to defines the pages element to be of type '#pcdata'
  • !element type use to defines the type element to be of type '#pcdata'
  • !element code use to defines the code element to be of type '#pcdata'

external dtd declaration

the declarations in the external subset are located in a separate text file.the external subset may be referenced via a public identifier and/or a system identifier.

the syntax

         <!doctype    root-element     system     "url or external file name">
         <!doctype    root-element     public       "external file path">

if you use the public keyword,you also need to use an fpi(which stands for formal public identifier).create a external.dtd file and save it.

             <!element         book    (author,price,pages,type,code) >
             <!element         author  (#pcdata) >
             <!element         price   (#pcdata) > 
             <!element         pages   (#pcdata) >
             <!element         type    (#pcdata) >
             <!element         code    (#pcdata) >

next step create a complete xml document, with an external dtd.

        <?xml  version="1.0"?>
             <!doctype         book   system  external.dtd >