Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Note also that the elements used are not part of any specification—they were made up to describe each part of the letter. The freedom to make up your own elements is one of XML’s chief strengths. What happens, however, when you try to share one of your XML documents with someone else? You understand what all the markup means, of course, but if you share with others who do not understand it, their XML parsers may not be able to handle your document correctly. Fortunately, you can share the XML structures you devise with others. By writing a Document Type Definition (DTD), you specify your XML elements, their attributes, and their syntax. Then you can reference your DTD at the top of your XML document so that any parser knows where to find the DTD file. After the parser has the DTD, it “understands” your document as well as you do. Composing a DTD is the subject of the next section.

Document Type Definitions (DTDs)

A DTD is a set of rules that specifies how to use XML markup. It contains specs for each element, including what the element’s attributes are, what values the attributes can take on, and what elements can be contained in others. Additionally, you can define entities in the DTD. You can define entities right in the DTD or in reference code in an external file.

For the business letter example in the preceding section, you might compose a DTD that looks like this:

<!ELEMENT letter (date, insideaddress, salutation, body, closing,
signature?)>
<!ELEMENT date (#PCDATA)>
<!ATTLIST date align (left|right) “left”>
<!ELEMENT insideaddress (#PCDATA | br*)>
<!ELEMENT br EMPTY>
<!ELEMENT salutation (#PCDATA)>
<!ELEMENT body (p+)>
<!ELEMENT p (#PCDATA)>
<!ATTLIST p align (left|justify|right) “left”>
<!ELEMENT closing (#PCDATA)>
<!ELEMENT signature (#PCDATA)>

This code may not be as clear as the markup it specifies, so here is a line-by-line description of what the DTD says:

  A LETTER has exactly one DATE, one INSIDEADDRESS, one SALUTATION, one BODY, one CLOSING, and an optional SIGNATURE.
  A DATE contains only text.
  The DATE element has an attribute called ALIGN that can take on values of LEFT and RIGHT. In the absence of a specification for ALIGN, its value should be taken to be equal to LEFT.
  An <INSIDEADDRESS> contains text and zero or more <BR/> elements.
  The <BR/> element is empty (that is, it has no end tag).
  A <SALUTATION> contains only text.
  The <BODY> comprises one or more <P> elements.
  The <P> element contains only text.
  The <P> element also has an ALIGN attribute that can equal LEFT, JUSTIFY, or RIGHT. Its default value is taken to be LEFT.
  The <CLOSING> element contains only text.
  The <SIGNATURE> element contains only text.

Just by knowing these rules, any XML parser could properly process your document. In addition to being useful for XML parsers and browsers, a DTD is useful in a few other situations:

  XML authoring tools use DTDs to validate the code you write. If the syntax in your XML document is incorrect, the program can detect and flag it for you. As the next section explains, the program should not even enable you to save the document unless it conforms to a DTD.
  An XML browser can’t know the default values of any attributes without a DTD.
  Just as with HTML, XML ignores extra whitespace characters (spaces, carriage returns, tabs) beyond a single space. Specifically, if an element’s content model is mixed (meaning it can be both text and other elements), whitespace within the element is taken to be significant. Elements that contain only other elements are said to have element content; whitespace in these elements is ignored.

Most importantly, DTDs enable you to publish your documents for consumption by others. To accomplish this, however, you must include instructions in your documents that tell an XML processing program how to find your DTD. Adding a simple <!DOCTYPE> element at the start of your XML file takes care of this. For the letter example, you might reference its DTD as follows:

<!DOCTYPE LETTER SYSTEM “http:/www.server.com/DTDs/letter.dtd”>
<LETTER>
<DATE ALIGN=”RIGHT”>
September 29, 1998
</DATE>
<INSIDEADDRESS>
...
</LETTER>

The first line in the code directs the XML processor to the DTD at http://www.server.com/DTDs/letter.dtd. The processor can then download the DTD and use it to check the document for appropriate syntax and to determine default attribute values.

Valid and Well-Formed XML Documents

For all the value a DTD provides, you may be surprised to learn that DTDs are not absolutely necessary. If an XML document conforms to a few key rules, a parser should still be capable of handling it. Specifically, a document is said to be well-formed if

  It contains one or more elements.
  The document conforms to the grammar put forward in the XML specification.
  One element called the root or document element is present whose start and end tags are not contained by any other element. The <LETTER> element would be the root element for the letter example.
  All other non-empty elements are properly nested.
  All attribute values are contained by quotation marks.
  All entities have either been declared in the DTD or, if no DTD is specified, are one of the following reserved entities: &amp;, &lt;, &gt;, &apos;, and &quot;.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.