Introduction to DTD's


 

Introduction to DTD's

In this tutorial you will learn about the Document Type Description (DTD) of an XML document.

In this tutorial you will learn about the Document Type Description (DTD) of an XML document.

Introduction to DTD's

To validate an xml document there should be some set of rules. DTD defines these set of rules i.e. defines the legal building blocks of an XML document and defines document structure with a list of legal elements and attributes.

DTD specifies that an xml can contain what elements and in which order, how the elements should be organized inside the other. It specifies that an element can contain what attributes.

A DTD can be defined inside an XML document, or an external reference can be declared.

Associating DTDs with XML document

DTD appears near the start of the xml document. Declaration can be done in two ways: inline in your XML document, or as an external reference.

1. Internal DTD

The dtd can be included in the target xml file. For example, the following file articlesdata.xml includes the dtd definition.

articlesdata.xml

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE articles [
<!ELEMENT articles (article)+>
<!ELEMENT article (title,url,img)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT img (#PCDATA)>
<!ATTLIST articles type CDATA #REQUIRED>
]>
<articles>
  <article>
    <title>What is XML?</title>
    <url>http://roseindia.net/tutorial/xml/what-is-xml.html</url>
    <img>http://roseindia.net/tutorialfiles/27000.invoicexml.gif</img>
  </article>
  <article>
    <title>Hello World XML</title>
    <url>http://roseindia.net/tutorial/xml/helloworldxml.html</url>
    <img>http://roseindia.net/tutorialfiles/27312.xml1.gif</img>
  </article>
</articles>

standalone = "yes" indicates that there are no declarations external to the document.

Rules:

  1. The document type declaration must be written in between the XML declaration and the root element.
  2. Keyword DOCTYPE must be followed by the root element.
  3. keyword DOCTYPE must be in upper case.

2. External DTD

External dtd is nothing different from internal dtd except defining definition in an external file. External DTD can be use with more than one XML document.

articles.dtd

<!ELEMENT articles (article)+>
<!ELEMENT article (title,url,img)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT img (#PCDATA)>
<!ATTLIST articles type CDATA #REQUIRED>

Specify the dtd file name in xml as given below. The syntax is:

<!DOCTYPE root-element SYSTEM "filename">

Now the xml file will look like:

articlesdata.xml

<?xml version="1.0" standalone="no"?>
<!DOCTYPE articles SYSTEM "articles.dtd">
<articles>
  <article>
    <title>What is XML?</title>
    <url>http://roseindia.net/tutorial/xml/what-is-xml.html</url>
    <img>http://roseindia.net/tutorialfiles/27000.invoicexml.gif</img>
  </article>
  <article>
    <title>Hello World XML</title>
    <url>http://roseindia.net/tutorial/xml/helloworldxml.html</url>
    <img>http://roseindia.net/tutorialfiles/27312.xml1.gif</img>
  </article>
</articles>

standalone = "no" indicates that external DTD must be processed along with all internal declarations.

Using Internal and External DTD simultaneously:

Internal and external DTD can be used together. This is useful when you need to put common definition in one place and want to add more locally in many files.

<?xml version="1.0" standalone="no"?>
<!DOCTYPE articles SYSTEM "articles.dtd" [
<!ELEMENT article (author)>
<!ELEMENT author (#PCDATA)>
]>

<articles>
  <article>
    <title>What is XML?</title>
    <url>http://roseindia.net/tutorial/xml/what-is-xml.html</url>
    <img>http://roseindia.net/tutorialfiles/27000.invoicexml.gif</img>
    <author>roseindia</author>
  </article>
  <article>
    <title>Hello World XML</title>
    <url>http://roseindia.net/tutorial/xml/helloworldxml.html</url>
    <img>http://roseindia.net/tutorialfiles/27312.xml1.gif</img>
    <author>roseindia</author>
  </article>
</articles>

Ads