An XML primer

The Extensible Markup Language (XML) has become the de facto standard for defining structured documents and data on the Web. VoiceXML builds upon the basic concepts and rules set forth by XML to define a logical vocabulary for describing interactive voice applications. This document explains how to create XML documents. With a grasp of the rules for creating XML documents you'll be ready to learn how to create VoiceXML documents.

1. Understanding XML documents

An XML document is comprised of one or more named elements organized into a nested hierarchy. An element is an opening tag, some data, and a closing tag. A tag is an element name preceded by a less-than symbol (<) and followed by a greater-than (>) symbol. For any given element, the name of the opening tag must match that of the closing tag. A closing tag is identical to an opening tag except that the less-than symbol is immediately followed by a forward-slash (/). Tag names are case-sensitive. Although the following examples employ lowercase, either case may be used as long as the opening and closing tag names are consistent.

<welcome>Welcome to Tellme University</welcome>

If an element does not contain any data, the opening and closing tags can be combined. Observe the location of the forward slash just prior to the greater-than (>) symbol.


An element may include zero or more attributes. An attribute specifies properties of the element that you modify and consists of a name/value pair. Attribute values must be contained in matching single or double quotes.

  <welcome type="texan">Howdy, partner</welcome>
  <welcome type="australian">G'day, mate</welcome>

Elements may be arranged in an infinitely nested hierarchy, but only one element in the document can be designated as the root document element. The root document element is the first element that appears in the document. With the exception of the comment element, discussed below, all other elements in the document must be children of the root element. In the following example, <customer> is the root element and it has three children. The child element <;address> has four children.

<customer id="C1234">
  <address type="biz">
    <street>1310 Villa Street</street>
    <city>Mountain View</city>

2. XML comments

As with any code, it's a good idea to document your work so that other programmers can understand it. A comment begins with the combination of characters "<!--" and ends with the combination of characters "-->". Comments may appear as a child of any element in an XML document. They can also appear before or after the root element. A comment may span multiple lines but cannot be nested. The following example includes a comment that identifies the parts of a bicycle.

 <part>A451</part> <!-- headset -->
 <part>C200</part> <!-- crank -->
 <part>X999</part> <!-- derailleur -->

3. XML validation

An XML schema can be formally defined using a Document Type Definition (DTD). The DTD defines the elements and attributes that are allowed in the document. The following DTD formally describes the schema for the customer example above.

<!ELEMENT customer (lname, fname, address+)>
<!ATTLIST customer id ID #REQUIRED>
<!ELEMENT lname (#PCDATA)>
<!ELEMENT fname (#PCDATA)>
<!ELEMENT address (street, city, state, zip)>
<!ATTLIST address type (biz | home) #REQUIRED>
<!ELEMENT street (#PCDATA)>
<!ELEMENT state (#PCDATA)>

An XML parser can typically be configured to validate an XML document against its corresponding DTD. A DTD is associated with an XML document via a DOCTYPE declaration. A DOCTYPE declaration can appear at the top of an XML document before the root element. In the following declaration, the document containing the root element <customer> is validated against a DTD located in the file "cust.dtd."

<!DOCTYPE customer SYSTEM "cust.dtd">

4. Reserved characters and CDATA sections

XML reserves certain characters, including less-than (<), greater than (>), and ampersand (&). To express these characters in your document data, use the equivalent character entity:

less-than (<) &lt;
greater than (>) &gt;
ampersand (&) &amp;

Here is an XML document fragment that uses character entities:

<equation> x &lt; 2 </equation>
<rhyme> Jack &amp; Jill </rhyme>

Alternatively, data can be protected from the XML parser in a CDATA section. Use a CDATA section to escape text that would otherwise be recognized as markup.

    <![CDATA[Abbott & Costello]]>

    <![CDATA[Laurel & Hardy]]>

    <![CDATA[Amos & Andy]]>

5. Comparing and contrasting XML and HTML

Some developers may be more familiar with Hypertext Markup Language (HTML) than XML. The following table lists some of the key differences between XML and HTML.

Can be used to define an arbitrary vocabulary for any problem domain. Defines a specific vocabulary used to create visually rendered documents.
All tags must be closed. Some tags, such as IMG and INPUT, need not be closed.
All attributes must be associated with a value. Some attributes don't require an explicit value (e.g., SCRIPT/@DEFER).
All attribute values must be surrounded by quotes. Attribute values need only be quoted when containing embedded spaces.
Entities beyond &, <, and > must be explicitly declared via ENTITY declarations in the DTD. Most entities are implicitly defined by the parser.
Parser is generally unforgiving. Basic rules about tags and attributes are enforced rigorously and DTD, if supplied, is followed to the letter. Parser is extremely forgiving and forgotten tags are implicitly inserted into the document.
When a DTD is supplied, unrecognized tags are rejected and the parse fails. Unrecognized tags are ignored.
May contain only a single root element. Should contain only a single root element (HTML), but the parser may be forgiving if the HTML element is omitted.

6. Additional reading

The following sites are comprehensive resources for late-breaking information about HTML, XML, and VoiceXML:

[24]7 Inc.| Terms of Service| Privacy Policy| General Disclaimers