|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Note also that the elements used are not part of any specificationthey were made up to describe each part of the letter. The freedom to make up your own elements is one of XMLs 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 elements 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:
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:
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 DocumentsFor 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
|
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. |