1 of 14

XML DTD

XML Document Type Declaration

2 of 14

XML Tree Structure

XML documents form a tree structure that starts at "the root" and branches to "the leaves".

3 of 14

XML Tree Structure

  • XML documents are formed as element trees.
  • An XML tree starts at a root element and branches from the root to child elements.
  • All elements can have sub elements (child elements):

<root>�  <child>�    <subchild>.....</subchild>�  </child>�</root>

  • The terms parent, child, and sibling are used to describe the relationships between elements.

XML Stores New Line as LF

  • Windows applications store a new line as: carriage return and line feed (CR+LF).
  • Unix and Mac OSX uses LF.
  • Old Mac systems uses CR.
  • XML stores a new line as LF.

Well Formed XML

  • XML documents that conform to the syntax rules above are said to be "Well Formed" XML documents.

4 of 14

Introduction

  • The XML Document Type Declaration, commonly known as DTD, is a way to describe XML language precisely.
  • DTDs check vocabulary and validity of the structure of XML documents against grammatical rules of appropriate XML language.
  • An XML DTD can be either specified inside the document, or it can be kept in a separate document and then liked separately.

5 of 14

Validating XML Document

XML Parser

XML DOCUMENT

XML Schema

Optimized XML Document

Error Messages

6 of 14

Syntax

Basic syntax of a DTD is as follows −

<!DOCTYPE element DTD identifier

[ declaration1 declaration2

........

]>

In the above syntax,

  • The DTD starts with <!DOCTYPE delimiter.
  • An element tells the parser to parse the document from the specified root element.
  • DTD identifier is an identifier for the document type definition, which may be the path to a file on the system or URL to a file on the internet. If the DTD is pointing to external path, it is called External Subset.
  • The square brackets [ ] enclose an optional list of entity declarations called Internal Subset.

7 of 14

Internal DTD

  • A DTD is referred to as an internal DTD if elements are declared within the XML files. To refer it as internal DTD, standalone attribute in XML declaration must be set to yes. This means, the declaration works independent of an external source.

Syntax

Following is the syntax of internal DTD −

  • <!DOCTYPE root-element [element-declarations]> where root-element is the name of root element and element-declarations is where you declare the elements.

8 of 14

Example- internal DTD

<?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>U.K.Roy</name>

<company>Web Technologies</company>

<phone>123456789</phone>

</address>

9 of 14

Rules

  • The document type declaration must appear at the start of the document (preceded only by the XML header) − it is not permitted anywhere else within the document.
  • Similar to the DOCTYPE declaration, the element declarations must start with an exclamation mark.
  • The Name in the document type declaration must match the element type of the root element.

10 of 14

External DTD

  • In external DTD elements are declared outside the XML file. They are accessed by specifying the system attributes which may be either the legal .dtd file or a valid URL.
  • To refer it as external DTD, standalone attribute in the XML declaration must be set as no. This means, declaration includes information from the external source.

Syntax

Following is the syntax for external DTD −

<!DOCTYPE root-element SYSTEM "file-name">

where file-name is the file with .dtd extension.

11 of 14

The following example shows external DTD usage

<?xml version = "1.0" encoding = "UTF-8"?>

<!DOCTYPE address SYSTEM "address.dtd">

<address>

<name>U.K.Roy</name>

<company>Web Technologies</company>

<phone>123456789</phone>

</address>

  address.dtd 

<!ELEMENT address (name,company,phone)> <!ELEMENT name (#PCDATA)> <!ELEMENT company (#PCDATA)> <!ELEMENT phone (#PCDATA)>

12 of 14

Building Blocks for XML DTD

  • ELEMENTS
  • ATTRIBUTES
  • ENTITIES
  • CDATA
  • PCDATA

13 of 14

ELEMENT TYPE DECLARATION

  • Element type declarations set the rules for the type and number of elements that may appear in an XML document, what elements may appear inside each other, and what order they must appear in

<!ELEMENT element-name category>�or�<!ELEMENT element-name (element-content)>

  • An element type cannot be declared more than once.
  • The name in the element type's end tag must match the name in the element type's start tag. Element names are case sensitive.
  • The keyword ELEMENT must be in upper case.

14 of 14

  • Standalone elements- also called as singleton elements, they cannot contain anything and as a consequence they are also called empty elements.

Ex: <hr><hr/>

<br/>

  • Simple Elements- they contain text or “parsed character DATA”(represented as #PCDATA in your DTD).

<!ELEMENT element_name (#PCDATA)>

  • Compound elements- these elements contain text, other (children) elements, or both text and other (children) elements.