Create a Document Type Definition (DTD)

Free HTML5 Game Tutorial for Beginners: Page 17

DTD Overview DTD Advantages Create a DTD DTD Header Element Overview Root Element Lists or Sets Composition Declare Elements The Whole DTD Put it all Together Tips Summary
DTD Icon

Overview

Most games include levels. XML files provide an easy method to load questions and answers for each level. Document Type Definition (DTD) files provide structure for XML files. Every XML file, which references a DTD file, conforms to the DTD file's structure. The Surface Game's DTD file declares elements and types.

This tutorial explains how to create a DTD file including the header, the root, elements for lists, elements composed of other elements, and the PCDATA type. This example includes the entire DTD file which demonstrates how elements, composition of elements, and types fit together. This tutorial also demonstrates how to apply a DTD file and components to an XML file.

It's helpful to create one DTD for use with multiple XML files. The Surface Game loads one DTD file for two levels. However hundreds of levels may use one DTD file.

XML and DTD Correlation

The following two diagrams color code properties declared in DTD and used in XML markup. The diagrams color code identical elements with identical background colors. The DTD diagram declares tags used within the XML diagram. For example the DTD lvl element and XML lvl tag are highlighted in blue. Both the DTD file and the XML file refer to the same lvl field. The DTD file describes element lvl. The XML file uses element lvl.

Color Coordinated DTD
DTD
Color Coordinated XML
XML

DTD Advantages

When individual XML files contain the information for one level, then games only need to download one XML file to prepare the next level. Downloading one file at a time reduces the amount of time to initialize the game or a new level.

Standard JavaScript includes methods to access data from an XML file. Therefore the developer has less code to write themselves, which reduces development time.

The Example Game

An XML file stores questions, answers, and image file paths for the example Surface Game. The DTD file describes the format and order for a set of questions, answers, and graphics.

How to Create a DTD

The DTD file is a simple text file which can be composed with any text editor.

DTD files include a header, elements and attributes, names and types. It's not recommended to use attributes with XML files. Therefore this tutorial focuses on elements, names, and types.

DTD header

The DTD file begins with a header. The header declares which version of XML should access the DTD, and what type of characters the DTD file contains. The header for Surface Game's DTD file displays below.

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

This example uses XML version 1.0, and UTF-8 character encoding. A character represents a single letter in computer languages. UTF means the DTD can include all characters defined with the Unicode set of characters. Unicode includes more than 110,000 characters and 100 language scripts such as English, Arabic, and Tibetan characters. The -8 which follows UTF in the header, means every character is represented with 8 bit bytes. A bit represents just zero or one (on or off), in computer hardware. Unicode requires 4 bytes per character. 8 bits x 4 (bytes) = 32 bits. Therefore the size of one Unicode character equals 32 bits.

DTD Elements

Elements, except the root element, are declared with a name and a type. The Surface Game uses the PCDATA type only. However other types are available.

Elements can be composed of other elements. The Surface Game uses a list of elements which are composed of other elements.

The Root Element

Every DTD needs a root element. A root element encloses all other elements. That means a DTD file includes only one root element. However the root element can contain many other elements.

This example names the root element lvl, however the root can have any name. In this example, lvl stands for level. Name DTD elements with any letters which give meaning to the file's structure.

The first element in a DTD, is the root element. The following line declares the root element.

<!ELEMENT lvl (ql+) >		
		

Lists or Sets

This short section discusses (ql+). Indicate lists or sets with +. ql means the root element will include an element named ql. (ql+) means the XML file contains one or more ql elements. In other words the XML file may include a list of ql elements.

The name ql to stands for the first two letters of the question list used in the Surface Game. Therefore the ql element declares a list of questions.

Declare Elements

Composition of elements refers to elements which contain other elements. Now declare what the ql element will contain. The ql element will contain three more elements. The elements are named q, a, and g. Therefore the ql element is one element, composed of other elements. The following line, within the DTD file, demonstrates declaring ql contains elements q, a and g.

<!ELEMENT ql (q,a,g) >

The letter q to represents one question. The letter a represents one answer. The letter g represents one graphic. Therefore each element of ql includes a question, an answer, and a graphic.

Next declare the type of data the q, a, and g elements contain. All three elements contain PCDATA.

PCDATA is character data ready for parsing. P stands for parsed. CDATA stands for character data. Parsed character data means the text between tags in an XML file will display as if it were added to an HTML file. Place any text in a PCDATA element that an HTML element accepts. For example a div element in an HTML Web page could include the text Question One. A PCDATA element in an XML file could also include the text Question One. The following line declares the q element will contain PCDATA.

<!ELEMENT q (#PCDATA) >

The Whole DTD File

The DTD file declares a root element called lvl, which contains a list of ql. Each ql contains the elements q, a, and g, in that exact order. The values assigned to q, a, and g must be of type PCDATA. The following listing is the entire levels.dtd DTD file used with the Surface Game.

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT lvl (ql+) >
<!ELEMENT ql (q,a,g) >
<!ELEMENT q (#PCDATA) >
<!ELEMENT a (#PCDATA) >
<!ELEMENT g (#PCDATA) >		
		

Put it all Together

DTD files indicate the structure XML files must contain. The example below displays an XML file referencing the Surface Game's DTD file named levels.dtd. Notice a set of ql tags. Each opening ql tag is <ql>. Each closing ql tag is </ql>. Within each ql tag notice a q, a, and g tag. Text appears between each q, a, and g tag. In XML Declare each opening <q> tag. Declare each closing </q> tag. Declare each opening <a> tag. Declare each closing </a> tag. Declare each opening <g> tag. Declare each closing </g> tag.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lvl SYSTEM "levels.dtd">
<lvl>
<ql>

<q>Find the area of the square.</q>	
<a>529</a>
<g>l1q1.gif</g>
 	
</ql>

<ql>

<q>Find the area of the rectangle.</q>
	
<a>22</a> 	 	 
<g>l1q2.gif</g> 
 		 			
</ql>
</lvl>		
		

Every element in the DTD starts with an opening tag such as <ql>. Every element ends with a closing tag such as </ql>. Each element between the ql tags displays in exactly the same order as it was declared in the DTD. First q, then a, then g.

Tips

It's helpful to keep XML files small for faster downloads. Use short names for elements to reduce file size. However one advantage to XML is the ability to provide understandable structure to files. Create names which make sense to the programmer for human friendly files. With the Surface Game example, new XML files download for each level played. JavaScript extracts values from elements in the XML file.

Summary

Most games include levels. XML files provide an easy method to load questions and answers for each level. Document Type Definition (DTD) files provide structure for XML files. Every XML file, which references a DTD file, conforms to the DTD file's structure. The Surface Game's DTD file declares elements and types.

This tutorial explained how to create a DTD file including the header, the root, elements for lists, elements composed of other elements, and the PCDATA type. This example included the entire DTD file which demonstrates how elements, composition of elements, and types fit together. This tutorial also demonstrated how to apply a DTD file and components to an XML file.

It's helpful to create one DTD for use with multiple XML files. The Surface Game loads one DTD file for two levels. However hundreds of levels may use one DTD file.

Tutorials on Create an XML File and Parse XML with JavaScript, explain how to use XML in a game.

Have fun and love learning! For the next tutorial, tap the image icon containing text which says Next Lesson, or the right pointing arrow at the bottom of the page.

Previous Page Next Page
Copyright © 2015 Seven Thunder Software. All Rights Reserved.