Read an Excerpt
Chapter 4: Understanding XML Document StructureIn This Chapter
- Mastering the basics of creating an XML document
- Exploring techniques for good XML coding
- Understanding the virtues of completeness
- Realizing that well-formedness implies more than it says
- Figuring out when you've got it right
In this chapter, we explore the meaning of the terms valid and well formed in relation to XML documents. Then we demonstrate good XML coding techniques. After you master the concept of good coding, you find out how complete XML delivers good structure overall. This leads to a discussion of what well formed really means in an XML document. After you get all that under your hat, you find out how to tell whether you've coded your XML correctly.
Mastering the Basics: Pieces and PartsWriting XML code is easy. All you need is a text editor -- you can even make up your own tag names! Before you begin creating XML documents, however, we need to talk about a few things.
You must understand a couple of buzzwords in order to create correctly formatted XML documents: valid and well formed. These two terms are often bantered about whenever techie types discuss XML. In this section, you uncover what these two terms mean -- without the usual jargon.
A well-formed road is a good road
All XML documents must be well formed -- end of story! A well-formed XML document follows a few specific rules to make the document easy for a computer program to read. See the section entitled "Making XML Well-Formed" later in this chapter for the complete scoop on creating well-formed XML.
Although all XML documents must be well formed -- or else they aren't XML -- not all XML documents have to be valid. This doesn't mean you don't want valid XML documents. Validity just adds another layer of structure that can be helpful when you want to use or reuse a document. After all, a structured document is better than one that can contain any old set of tags in any old order.
A valid XML document adheres to a Document Type Definition (DTD). To answer the next inevitable question, a DTD defines a set of rules that specify which tags can legally appear in your document. A DTD also spells out how those tags can appear: It indicates their order and which tags can appear within other tags. Simply put, a DTD tells you what your tags can and cannot do; therefore, a DTD defines the rules that XML markup in a document must follow.
DTDs are not necessary, or required, for every instance of XML. However, they do give your XML documents an extra layer of organizational structure that can make your life easier. DTDs also help developers share expectations about document structures. If you're interested in reading more about DTDs, please see Chapter 5.
Now that you know what to look out for, open your text editor so we can get started.
Preambles and Definitions Start the GameA well-formed XML document always begins with a declaration that announces that the document is written using XML 1.0, so speak up and declare yourself!
A declaration for an XML document is simple. It occurs in the first line of your XML document. Currently, a declaration must point to XML version 1.0 -- after all, it's the only version out there. An XML declaration looks like this:
To be honest, you don't have to include an XML declaration in every XML document. If you don't point to a particular version, XML 1.0 is the default. However, we suggest that you get in the declaration habit for all XML documents because when version 2.0 comes out, XML declarations might be required.
You can write fancier declarations for XML documents if you want. For more XML declaration details, see Chapter 2.
If you decide to use a DTD to add organization and structure to an XML document, you must declare that DTD. (Remember that a DTD defines the rules of the game for your XML document. To find out more about creating and using DTDs, consult Chapter 5.) To declare a DTD in an XML document, you need just one simple line:
Document-Name SYSTEM "Document-Name.dtd">
There you have it -- one line of code that says it all for the related DTD!
Good Bodies Make for Good ReadingHave you ever picked up an instruction manual and wished you hadn't? You know the ones we mean -- chock full of grammatical errors, poor syntax, and just plain lousy sentence structure. After hours of struggling to finish assembling a do-it-yourself bookcase, you wonder why the author took the time to write a manual at all. You might be tempted -- and rightfully so -- to make the same value judgment about XML code. If the code lacks structure from a human-readable standpoint, perhaps the document containing that code is of poor quality.
This section explains how to write good -- that is, well-formed -- XML code to keep others from saying bad things about your work later!
The root of the matter
In HTML, you begin every HTML document with . The . . . tag pair is the root element for an HTML document. With XML, you create elements just like you use predefined HTML tags. The only difference with XML is that you get to choose the name for your root element. For example...