KickStart Tutorial XML by Jan Kampherbeek - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

XML: the DTD

Defining the language

To use XML you need a DTD (Document Type Definition). A DTD contains the rules for a particular type of XML-documents. Actually it's the DD that defines the language.

Elements

A DTD describes elements. It uses the following syntax:
The text <! ELEMENT, followed by the name of the element, followed by a description of the element.
For instance:
<!ELEMENT brand (#PCDATA)>
This DTD description defines the XML tag <brand>.

Data

The description (#PCDATA) stands for parsed character data.
It's the tag that is shown and also will be parsed (interpreted) by the program that reads the XML document.
You can also define (#CDATA), this stands for character data.
CDATA will not be parsed or shown.

Sub elements

An element that contains sub elements is described thus: <!ELEMENT car (brand, type) >
<!ELEMENT brand (#PCDATA) >
<!ELEMENT type (#PCDATA) >

<!ELEMENT type (#PCDATA) >

 

This means that the element car has two subtypes: brand and type. Each subtype can contain characters.

Number of sub elements

If you use <!ELEMENT car (brand, type) >, the sub elements brand and type can occur once inside the element car. To change the number of possible occurrences the following indications can be used:

+ must occur at least one time but may occur more often
* may occur more often but may also be omitted
? may occur once or not at all

The indications are used behind the sub element name. For instance: <!ELEMENT animal (color+) >

Making choices

With the sign '|' you define a choice between two sub elements. You enter the sign between the names of the sub elements. <!ELEMENT animal (wingsize|legsize) >

Empty elements

Empty elements get the description EMPTY.
For instance
<!ELEMENT separator EMPTY>
that could define a separator line to be shown if the XML document appears in a browser.

DTD: external

A DTD can be an external document that's referred to. Such a DTD starts with the text
<!DOCTYPE name of root-element SYSTEM "address"> The address is an URL that points to the DTD.

The address is an URL that points to the DTD.

In the XML document you make clear that you'll use this DTD with the line: <!DOCTYPE name of root-element SYSTEM "address">
that should be typed after the line <?xml version="1.0"?>

DTD: internal

A DTD can also be included in the XML document itself. After the line <?xml version="1.0"?> you must type <!DOCTYPE name of root-element [
followed by the element definitions.
The DTD part is closed with
]>