DTD Declaration

DTD declaration either internal XML document or make external DTD file, after linked to a XML document.

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.

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/External DTD Declaration


DTD Declaration

Internal DTD Declaration

Internal DTD you can declare inside your XML file. In XML file top <!DOCTYPE ... > declaration to declare the DTD.

<?xml version="1.0" standalone="yes"?>
    <!DOCTYPE root_element [
    ....
    ....
  ]<

Following internal DTD example define root element <employee> along with id attribute and other element are second level element along withdiscipline attribute.

DTD rules must be placed specifies top of the XML element (root element) in the document.

Example

internal_dtd.xml
<?xml version="1.0" standalone="yes"?>
  <!DOCTYPE employee [
      <!ELEMENT employee (name, designation, email)>
  <!ATTLIST employee 
    id CDATA  #REQUIRED>
      <!ELEMENT name (#PCDATA)>
      <!ELEMENT designation (#PCDATA)>
  <!ATTLIST employee 
    discipline CDATA  #IMPLIED>
      <!ELEMENT email (#PCDATA)>
  ]>

<employee id="1">
  <name>Opal Kole</name>
  <designation discipline="Web developer" >Senior Engineer</designation>
  <email>[email protected]</email>
</employee>

External DTD Declaration

External DTD are shared between multiple XML documents. Any changes are update in DTD document effect or updated come to a all XML documents.

External DTD two type:


Private DTD Private DTD identify by the SYSTEM keyword. Access for single or group of users.

You can specifies rules in external DTD file .dtd extension. Later in XML file <!DOCTYPE ... > declaration to linking this DTD file.

Syntax

<!DOCTYPE root_element SYSTEM "dtd_file_location">

Example

external_dtd.dtd
<!DOCTYPE employee [
      <!ELEMENT employee (name, designation, email)>
  <!ATTLIST employee 
    id CDATA  #REQUIRED>
      <!ELEMENT name (#PCDATA)>
      <!ELEMENT designation (#PCDATA)>
  <!ATTLIST employee 
    discipline CDATA  #IMPLIED>
      <!ELEMENT email (#PCDATA)>
  ]>
main.xml
<?xml version="1.0" standalone="no"?>
<!DOCTYPE root_element SYSTEM "external_dtd.dtd">
<employee id="1">
  <name>Opal Kole</name>
  <designation discipline="Web developer">Senior Engineer</designation>
  <email>[email protected]</email>
</employee>


Public DTD Public DTD identify by the PUBLIC keyword. Access any users and our XML editor are know the DTD. Some common DTD Web DTD, XHTML, MathML etc.

Syntax

<!DOCTYPE root_element PUBLIC "dtd_name" "dtd_file_location">

Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    ...
  </head>
  <body>
    ...
    ...
  </body>
</html>