Introduction to XML - Understanding XML Rules (1-3)
(Page 4 of 7 )
What do you need to do to create an XML document? At the simplest level, you need a text editor in which you can type. On the other end of the spectrum, you may need an XML editing program; a validation tool; a DTD and DTD editor; XSL or CSS for formatting; and a repository for storage, tracking, and retrieval.
In this section, I cover simple rules that XML documents need to If you follow XML follow. Examples in this section serve to clarify the rules of XML, with each addressing a specific aspect of XML. The numbering on the rules (Rule 1, Rule 2, ...) is just to help you in your reading.
Rule 1: If you have an XML declaration, it must be the first line of the document.
While an XML declaration is not required, it can be helpful when bringing XML into different tools. The XML declaration is located at the top of your XML file and helps tools (and readers) identify the file as XML, rather than SGML or some other markup. An XML declaration may look like this:
<?xml version="1.0"?>
If you have many small XML documents-XML fragments-that you plan to store and assemble, you might want to omit the declaration.That way, you do not end up with multiple declarations in an assembled file. This scenario will cause an error, since the declaration cannot occur below any content.
If you plan to produce valid XML, please note that you must have a declaration for an XML document to be valid.
Rule 2: All XML documents must have a root element.
In the example below, the root element is <zoo>. This root element begins the XML and its corresponding end tag </zoo> comes after all the content in the document, as shown in the following code:
<?xml version="1.0"?>
<zoo>
Zoo Document Content
</zoo>
Rule 3: All XML elements must be properly nested within the root element.
Items are nested inside the <zoo> root, as shown in the following code. These nested items include an <animals> child element. Inside this child element are additional elements called and . These are nested child elements.
<?xml version="1.0"?>
<zoo>
<animals>
<cat>Leo</cat>
<reptile>Pudgie</reptile>
</animals>
</zoo>
NOTE: Spaces in front of the lines of code, as used in examples, are to help you see the beginning and end tags and the element nesting. This spacing is not required.
The preceding code snippet shows proper nesting. The tags begin and end without intermingling with other tags. An improper version might look like the following code:
<?xml version="1.0"?>
<zoo>
<animals>
<cat>Leo
<reptile>Pudgie</cat></reptile>
</animals>
</zoo>
What is incorrect is that the end tag is inside the reptile start and end tags. The nesting is incorrect because the tags cannot fold inside other tags unless they do so completely. Mathematically speaking, it is the equivalent of mixing up parentheses in a grouping, as illustrated in the following example:
(x + y)/z
does not have the same meaning as:
(x + y/z)
In these equations, having the parentheses in different locations changes what is divided by z and thereby change the value of the equation. In the first equation, x + y is divided by z, while in the second equation y alone is divided by z and then the result is added to x. So, placement of parentheses is important to getting the correct result. As with the equation above, to produce appropriate structure in XML it is necessary to properly nest the beginning and end tags.
This chapter is from XML and Framemaker, by Kay Ethier (Apress, 2004, ISBN: 159059276X). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Understanding XML Rules (4-5) >>
More XML Articles
More By Kay Ethier