DTD tutorial

The educational technology and digital learning wiki
Jump to navigation Jump to search

Introduction

This is a short tutorial about DTDs. It brievely shows how to read DTDs, then how to create these.

A note on its production: This spring I am teaching a little course at Webster, Geneva. I usually do my teaching slides with Framemaker (e.g. see my french slides. Now I wanted to know how long it takes to translate them into wiki. Answer: about 90 minutes (way too long!). I first export to HTML and then apply a html2wiki filter and then hand edit. Of course, this result also needs some editing, slides are not meant for self-study .... - 18:46, 23 April 2007 (MEST).

DTD grammars are a set of rules that define:

  • a set of elements (tags) and their attributes that can be used to create an XML document;
  • how elements can be embedded ;
  • different sorts of entities (reusable fragments, special characters).
  • DTDs can't define what the contents look like, i.e. character data (element contents) and most attribute values.

Specification of a markup language

  • The most important part is usually the DTD, but in addition other constraints can be added !
  • The DTD does not identify the root element ! You have to tell the users what elements can be root elements.
  • Since DTDs can't express data constraints, write them out in a specification document, e.g. "the value of length attribute is a string composed of a number plus "cm" or "inch" or "em". Exemple
<size length="10cm">

Example 1: A simple DTD

 <!ELEMENT page  (title, content, comment?)>
 <!ELEMENT title (#PCDATA)>
 <!ELEMENT content (#PCDATA)>
 <!ELEMENT comment (#PCDATA)>
  • A DTD document contains just rules .... nothing else (see later for explanations)


Using a DTD with an XML document

Document type declarations

  • A valid XML document includes a declaration that specifies the DTD used
  • DTD is declared on top of the file after the XML declaration.
  • XML declarations, DTD declaration etc. are part of the prologue
  • So: The <!DOCTYPE...> declaration is part of the XML file, not the DTD ....
Example
 <?xml version="1.0" " ?>
 <!DOCTYPE hello SYSTEM "hello.dtd">
 <hello>Here we <strong>go</strong> ... </hello>
Four ways of using a DTD

(1) No DTD (XML document will just be well-formed) (2) DTD rules are defined inside the XML document

  • We get a "standalone" document (the XML document is self-sufficient)

(3) "Private/System" DTDs, the DTD is located on the system (own computer or the Internet). That's what you are going to use when you write your own DTDs.

 <!DOCTYPE hello SYSTEM "hello.dtd">

(4) Public DTDs, we use a name for the DTD. This means that both your XML editor and user software know the DTD. This is the strategy used for common Web DTDs like XHTML, SVG, MathML, etc.

Syntax of the DTD declaration in the XML document

A DTD declaration starts with the keyword "DOCTYPE"
  <!DOCTYPE ....  >
... followed by the root element
Remember that DTDs don't know their root element, root is defined in the XML document ! Also note that DTDs must define this root element just like any other element ! (you can have more than one)
 <!DOCTYPE hello .... >
... followed by the DTD definition or a reference to a DTD file
Syntax for internal DTDs (only !)
DTD rules are inserted between brackets [ ... ]
    <!DOCTYPE hello ['
        <!ELEMENT hello (#PCDATA)>
        ]>
Syntax to define "private" external DTDs
  • DTD is identified by the URL after the " SYSTEM " keyword
 <!DOCTYPE hello '' SYSTEM "hello.dtd"'' >
Syntax for public DTDs
  • after the " PUBLIC " keyword you have to specify an official name and a backup URL that a validator could use.
 <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
  "http://my.netscape.com/publish/formats/rss-0.91.dtd"'' >


Some examples of XML documents with DTD declarations

Example 2
Hello XML without DTD
 <?xml version="1.0" standalone="yes"?>
 <hello> Hello XML et hello cher lecteur ! </hello>
Example 3
Hello XML with an internal DTD
  <?xml version="1.0" standalone="yes"?>
  <!DOCTYPE hello [
     <!ELEMENT hello (#PCDATA)>
     ]>

  <hello> Hello XML et hello dear readers ! </hello>
Example 4
Hello XML with an external DTD
That's what you should with your own home-made DTDs
 <?xml version="1.0" ?>
 <!DOCTYPE hello SYSTEM "hello.dtd">
 <hello> This is a very simple XML document </hello>
Example 5
XML with a public external DTD (RSS 0.91)
 <?xml version="1.0" "?>
 <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
  "http://my.netscape.com/publish/formats/rss-0.91.dtd">
 <rss version="0.91">
 <channel> ...... </channel>
 </rss>

Understanding DTDs by example

Recall that DTDs define all the elements and attributes and the way they can be combined

Example 6: Hello text with XML

A simple XML document of type <page>
<page>
 <title>Hello friend</title>
 <content>Here is some content :)</content>
 <comment>Written by DKS/Tecfa, adapted from S.M./the Cocoon samples</comment>
</page>
A DTD that would validate the document
Xml-intro-edit-6.png

Example 7: A recipe list in XML

  • Source: Introduction to XML by Jay Greenspan (now dead URL)
 <?xml version="1.0"?>
 <!DOCTYPE list SYSTEM "simple_recipe.dtd">
 <list>
 <recipe>
   <author>Carol Schmidt</author>
   <recipe_name>Chocolate Chip Bars</recipe_name>
   <meal>Dinner</meal>
   <ingredients>
     <item>2/3 C butter</item>      <item>2 C brown sugar</item>
     <item>1 tsp vanilla</item>     <item>1 3/4 C unsifted all-purpose flour</item>
     <item>1 1/2 tsp baking powder</item>
     <item>1/2 tsp salt</item>      <item>3 eggs</item>
     <item>1/2 C chopped nuts</item>
     <item>2 cups (12-oz pkg.) semi-sweet choc. chips</item>
   </ingredients>
   <directions>
Preheat oven to 350 degrees. Melt butter; combine with brown sugar and vanilla in  large mixing bowl. Set aside to cool. Combine flour, baking powder, and salt; set  aside. Add eggs to cooled sugar mixture; beat well. Stir in reserved dry  ingredients, nuts, and chips. Spread in greased 13-by-9-inch pan.
Bake for 25 to 30 minutes until golden brown; cool.  Cut into squares.
   </directions>
 </recipe>
</list>
Contents of the DTD

Xml-intro-edit-7.png

Example 8: A simple story grammar

 <?xml version="1.0" "?>
 <!-- DTD to write simple stories
      Made by Daniel K. Schneider / TECFA / University of Geneva
      VERSION 1.0
      30/10/2003 -->
 <!ELEMENT STORY (title, context, problem, goal, THREADS, moral, INFOS)>
 <!ATTLIST STORY xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">
 <!ELEMENT THREADS (EPISODE+)>
 <!ELEMENT EPISODE (subgoal, ATTEMPT+, result) >
 <!ELEMENT ATTEMPT (action | EPISODE) >
 <!ELEMENT INFOS ( ( date | author | a )* ) >
 <!ELEMENT title (#PCDATA) >
 <!ELEMENT context (#PCDATA) >
 <!ELEMENT problem (#PCDATA) >
 <!ELEMENT goal (#PCDATA) >
 <!ELEMENT subgoal (#PCDATA) >
 <!ELEMENT result (#PCDATA) >
 <!ELEMENT moral (#PCDATA) >
 <!ELEMENT action (#PCDATA) >
 <!ELEMENT date (#PCDATA) >
 <!ELEMENT author (#PCDATA) >
 <!ELEMENT a (#PCDATA)>
 <!ATTLIST a
      xlink:href CDATA #REQUIRED
      xlink:type CDATA #FIXED "simple">
Here is a valid skeleton
  <?xml version="1.0" " ?>
  <!DOCTYPE STORY SYSTEM "story-grammar.dtd">
  <?xml-stylesheet href="story-grammar.css" type="text/css"?>
  <STORY>
   <title>The little XMLer</title>
   <context></context>
  <problem></problem>
  <goal></goal>
  <THREADS>
    <EPISODE>
      <subgoal>I have to do it ...</subgoal>
      <ATTEMPT>
        <action></action>
      </ATTEMPT>
      <result></result>
    </EPISODE>
  </THREADS>
  <moral></moral>
  <INFOS>
  </INFOS>
 </STORY>

The picture gives some extra information

Xml-intro-edit-8.png

Example 9: Lone family DTD

Xml-intro-edit-9.png

A valid XML file
 <?xml version="1.0" ?>
 <!DOCTYPE family SYSTEM "family.dtd">
 <family>
   <person name="Joe Miller" gender="male"
           type="father" id="123.456.789"/>
   <person name="Josette Miller" gender="female"
           type="girl" id="123.456.987"/>
 </family>

Example 10: RSS

  • There are several RSS standards. RSS 0.91 is Netscape's original (still being used)
 <!ELEMENT rss (channel)>
 <!ATTLIST rss version CDATA #REQUIRED>
  <!-- must be "0.91"> -->
  <!ELEMENT channel (title | description | link | language | item  | rating? | image? | textinput? | 
               copyright? | pubDate? | lastBuildDate? | docs? | managingEditor? | 
               webMaster? | skipHours? | skipDays?)*>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT description (#PCDATA)>
  <!ELEMENT link (#PCDATA)>
  <!ELEMENT image (title | url | link | width? | height? | description?)*>
  <!ELEMENT url (#PCDATA)>
  <!ELEMENT item (title | link | description)*>
  <!ELEMENT textinput (title | description | name | link)*>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT rating (#PCDATA)>
  <!ELEMENT language (#PCDATA)>
  <!ELEMENT width (#PCDATA)>
  <!ELEMENT height (#PCDATA)>
  <!ELEMENT copyright (#PCDATA)>
  <!ELEMENT pubDate (#PCDATA)>
  <!ELEMENT lastBuildDate (#PCDATA)>
  <!ELEMENT docs (#PCDATA)>
  <!ELEMENT managingEditor (#PCDATA)>
  <!ELEMENT webMaster (#PCDATA)>
  <!ELEMENT hour (#PCDATA)>
  <!ELEMENT day (#PCDATA)>
  <!ELEMENT skipHours (hour )>
  <!ELEMENT skipDays (day )>
Possible XML document for RSS
  <?xml version="1.0" " ?>
  <!DOCTYPE rss SYSTEM "rss-0.91.dtd">
  <rss version="0.91">
     <channel>
       <title>Webster University</title>
       <description>Home Page of Webster University</description>
       <link>http://www.webster.edu</link>
       <item>
	 <title>Webster Univ. Geneva</title>
	 <description>Home page of Webster University Geneva</description>
	 <link>http://www.webster.ch</link>
       </item>
       <item>
	 <title>http://www.course.com/</title>
	 <description>You can find Thomson text-books materials (exercise data) on this web site</description>
	 <link>http://www.course.com/</link>
       </item>
     </channel>
   </rss>

Definition of elements

Syntax of a DTD rule to define elements:

 <!ELEMENT tag_name (child_element_specification) >

Child_element_specification may contain:

  • A combination of child elements according to combination rules
<!ELEMENT page  (title, content, comment?)>
  • Mixed contents, i.e. child elements plus #PCDATA or ANY
<!ELEMENT para (strong | #PCDATA )*>
  • #PCDATA (Just data)
<!ELEMENT title (#PCDATA)>
  • ANY (only used during development)
<!ELEMENT para (ANY)*>
  • EMPTY (the element has no contents)
<!ELEMENT person EMPTY>

Tag names

  • Each tag name must start with a letter or an underscore ('_')
    followed by letters, numbers or the following characters: '_' , '-', '.', ':'
BAD example:
  <!ELEMENT 1st ...>
BAD example:

 <!ELEMENT My Home ...>

Combination rules

A and B = tags

Explanation

DTD examples

XML examples

A , B

A followed by B

Elements in that order

<!ELEMENT person ( name ,email? )>
<!ELEMENT Name (First, Middle, Last)>
 <person>
   <name>Joe</name>
   <email>x@x.x</email>
 </person>
 <Name>
   <First>D.</First><Middle>K.</Middle><Last>S.</Last>
 </Name>

A?

A is optional,

(it can be present or absent)

<!ELEMENT person  (name,  email? )>
<!ELEMENT Name (First,Middle?,Last)>
 <person>
  <name>Joe</name>
</person>
 <Name><First>D.</First><Last>S.</Last></Name>

A

At least one A

<!ELEMENT person (name,  email+ )>
<!ELEMENT list (movie+)
 <person> <name>Joe</name>
   <email>x@x.x</email></person>
 <person> <name>Joe</name>
   <email>x@x.x</email>
   <email>x@y.x</email>
 </person>
 <list>
    <movie>Return of ...</movie>
    <movie>Comeback of ...</movie> 
 </list>

A*

Zero, one or several A

<!ELEMENT person  (name,  email* )>
<!ELEMENT list (item*)
 <person>
   <name>Joe</name>
  </person>
  <list>
   <item>Return of ...</item>
  </list>

A | B

Either A or B

<!ELEMENT person ( email | fax )>
<!ELEMENT major (economics | law)>
 <person> <name>Joe</name>
   <email>x@x.x</email>
 </person>
 <person> <name>Joe</name>
   <fax>123456789</fax>
 </person>
 <major> 
   <economics> </economics>
 </major>

(A, B)

Parenthesis will group and you can apply the above combination rules to the whole group

<!ELEMENT text (para | list | title)*>
  <text>
    <title>Story</title>
    <para>Once upon a time</para> 
    <title>The awakening</title> 
    <list> ... </list>
 </text>

Special contents

Special elements

Explanation

DTD examples

XML example

#PCDATA

"Parsed Character Data"

Text contents of an element. It should not contain any <,>,& etc.

<!ELEMENT email (#PCDATA)>
 <email>Daniel.Schneider@nowhere.org</email>
ANY

Allows any non-specified child elements and parsed character data (avoid this !!!)

<!ELEMENT person ANY>
 <person>
   <c>text</c>
   <a>some <b>bbb</b>inside</a>
 </person>
EMPTY
No contents
<!ELEMENT br EMTPY>

Note about Mixed Content

  • Mixed element contents contain both text and tags.
 <para> here is <a href="xx">link</a>. <b>Check</b> it out </para>
  • You have to use the "|" construct for these
Good examples
 <!ELEMENT para (#PCDATA|a|ul|b|i|em)* >  
 <!ELEMENT p (#PCDATA | a | abbr | acronym | br | cite | code | dfn | em | img | kbd |
           q | samp | span | strong | var )* >  
 <!ELEMENT par (#PCDATA | %font; | %phrase; | %special; | %form;)* >
Bad examples
 <!ELEMENT p (name, first_name, #PCDATA)*>
 <!ELEMENT p ( (#PCDATA) |a|ul|b|i|em)*>

Defining attributes

Rough syntax of Attribute rules:

<!ATTLIST element_name attr_name Attribute_type Type_Def Default >

Overview:

Keyword

Attribute types

CDATA

"Character Data" - Text data

NMTOKEN

A single word (no spaces or punctuations)

ID

Unique identifier of the element.

IDREF

Reference to an identifier.

IDREFS

Reference to one or more identifiers

(A|B|C|..)

List of values (from which the user must choose)

Type Definition

#IMPLIED

Attribute is optional

#REQUIRED

Attribute is mandatory)

#FIXED Value

Attribute has a fixed value (user can't change it)

Illustrations

DTD rule

example XML

<!ATTLIST person first_name CDATA #REQUIRED>

<person first_name="Joe">

<!ATTLIST person gender (male|female) #IMPLIED>

<person gender="male">

<!ATTLIST form method CDATA #FIXED "POST">

<form method="POST">

<!ATTLIST list type (bullets|ordered) "ordered">

<list type="bullets">

<!ATTLIST sibling type (brother|sister) #REQUIRED>

<sibling type="brother">

<!ATTLIST person id ID #REQUIRED>

<person id="N1004">

Shortcut to define multiple attributes for an element:

<!ATTLIST target_tag
         attr1_nom TypeAttribut TypeDef Defaut
         attr2_nom TypeAttribut TypeDef Defaut
...>
Shortcut illustrations
 <!ATTLIST person ident ID #REQUIRED 
      gender male|female) #IMPLIED
      nom CDATA #REQUIRED 
      prenom CDATA #REQUIRED   
      relation  brother|sister) #REQUIRED >  
 <!ATTLIST portable owner IDREF #REQUIRED >

Example: Lone family DTD (file family.dtd)

Xml-intro-edit-10.png

A valid family XML file
 <?xml version="1.0" ?>
 <!DOCTYPE family SYSTEM "family.dtd">
 <family>
   <person name="Joe Miller" gender="male"
           type="father" id="N123456789"/>
   <person name="Josette Miller" gender="female"
           type="girl" id="N123456987"/>
 </family>

Entities

General entities

Consider entities as abbreviations for some other content. An entity must be defined in the DTD and its contents are substituted when encountered in the XML file. Then, recall that XML initially only defines 5 entities and that HTML does many more...

  • Use the &lt; &amp; &gt; &aquot; &apos; entities to refer to <, &, >," and

Syntax of an internal entity definition: <!ENTITY entity_name "content">

Syntax of an external entity definition: <!ENTITY entity_name SYSTEM URI>

Syntax of using an entity: &entity_name;

Illustrations of entity definitions

DTD rule

XML example

Result

<!ENTITY jt "Joe Test">

<para> &jt; is here<para>
<para> Joe Test is here</para>

<!ENTITY space "&#160;">

<!ENTITY copyright "&#xA9;">

&copyright; D. Schneider

<!ENTITY explanation SYSTEM "project1a.xml">

<citation> &explanation; </citation>

<citation> contents of project1a.xml ... </citation>

Parameter entities

Most professional DTDs use parameter entities that are used to simplify DTD writing. More complex DTD often use same structures all over. Instead of typing these several times for each element definition, one can use an ENTITY construction like these:

 <!ENTITY  % entity_name "content">
 <!ENTITY  % entity_name SYSTEM "URI">

Example DTD entities to define reusable child elements

 <!ENTITY % Content "(Para | List | Listing)*">

Later in the DTD we then can have element definitions like this:

 <!ELEMENT Intro (Title, %Content; ) >
 <!ELEMENT Goal (Title, %Content; ) >

The XML parser will then simply translate these %Content; and we get:

 <!ELEMENT Intro (Title, (Para | List | Listing)*) >
 <!ELEMENT Goal (Title, (Para | List | Listing)* ) >

Example 13 DTD entities to define reusable attribute definitions

  • You may use the same procedure to define "bricks" for attribute definitions
  • Entity example that defines part of an attribute definition
 <!ENTITY % stamp '
   id ID #IMPLIED
   creation-day NMTOKEN #IMPLIED
   .......
   mod-by NMTOKEN #IMPLIED
   version NMTOKEN #IMPLIED
   status (draft|final|obsolete) #IMPLIED
   approval (ok|not-ok|so-so) #IMPLIED
   main-author CDATA #IMPLIED
   '>

ATTLIST definitions below use %stamp;

 <!ELEMENT main-goal (title, content, (after-thoughts)?, (teacher-comments)?)>
 <!ATTLIST main %stamp; >
 <!ELEMENT title (...)>
 <!ATTLIST main %stamp; >

Some advice for designing DTDs

General advice

Don't forget elements and be liberal
  • Each element needs to be defined, but only once !
  • Only make elements mandatory if they really are wanted, else use e.g. element  ?
Plan the global structure
  • Before you start writing out DTDs, use some simple "language" to draft the structure, e.g. use a notation like:
name   ==>  family given
family ==> "text"
  • In most cases, each "object" of your "information domain" becomes an element
  • Use child elements to model components
  • Use attributes to describe properties of components
Start from the root element and work your way down
  1. Root element
  2. Child elements of root element
  3. Child elements of the other elements, etc.

Attributes vs. Elements

  • There are some design rules that may help you decide whether using an element or an attribute
  • In case of doubt, always use elements ...
Rather use child elements inside an element to represent an information block
  • if order is important (attributes can't be ordered)
  • if you plan to use the same kind of information block with different parents
  • if a future version of DTD may specify sub-components of an information block
  • if the information block represents a "thing" (an object in OO programming)
  • if the DTD is text-centric, because an author must see contents she/he edits and attributes are often hidden away in XML editors; only use attributes to qualify properties like style !
Rather use attributes to represent an information block
  • if an attribute refers to an other element:
<pet_of owner_name="lisa" pet_type="cat"> would refer to <animal category="cat">
  • to declare usage/type/etc. of an element:
 <address usage="prof"> ... </address>
  • if you wish to list all possible values a user can enter
  • if you want to restrict data type of the attribute value (e.g. require a single word)