XML documents, like HTML documents, are comprised of tags and data. One big difference between the two documents, however, is that the tags used by an XML document are created by the author. Another big difference is that an XML document stores and describes that data; it doesn't do anything more with the data, such as display it, like an HTML document does.
<?xml version="1.0"?> <wonder> <name>Colossus of Rhodes</name> <location>Rhodes, Greece</location> <height units="feet">107</height> </wonder>
XML documents should be rather self-explanatory in that the tags should describe the data they contain (Figure 1.1).
The first line of the XML document is the XML declaration which
notes which version of XML you are using. The next line
The next 3 lines are called child elements, and they describe the root element in more detail.
The last child element, height, contains an attribute called units which is being used to store the specific units of the height measurement. Attributes are used to include additional information to the element, without adding text to the element itself.
Finally, the XML document ends with the closing tag of the root element </wonder>.
This is a complete and valid XML document. Nothing more needs to be written, added, annotated, or complicated. Period.
Rules for Writing XML
XML has a structure that is extremely regular and predictable. It is defined by a set of rules, the most important of which are described below. If your document satisfies these rules, it is considered well-formed. Once a document is considered well-formed, it can be used in many, many ways.
<?xml version="1.0"?> <wonder> <name>Colossus of Rhodes</name> </wonder>
A root element is required. Every XML document must contain one, and only one, root element. This root element contains all the other elements in the document. The only pieces of XML allowed outside (preceding) the root element are comments and processing instructions (Figure 1.3).
<?xml version="1.0"?> <wonder> <name>Colossus of Rhodes</name> <main_image file="colossus.jpg"/> </wonder>
Closing tags are required. Every element must have a closing tag. Empty elements can use a separate closing tag, or an all-in-one opening and closing tag with a slash before the final > (Figure 1.4).
Elements must be properly nested. If you start element A, then start element B, you must first close element B before closing element A (Figure 1.4).
<name>Colossus of Rhodes</name> <Name>Colossus of Rhodes</Name> <name>Colossus of Rhodes</Name>
Values must be enclosed in quotation marks. An attribute's value must always be enclosed in either matching single or double quotation marks (Figure 1.6).
Elements, Attributes, and Values
XML uses the same building blocks as HTML: tags that define elements, values of those elements, and attributes. An XML element is the most basic unit of your document. It can contain text, attributes, and other elements. An element has an opening tag with a name written between less than (<) and greater than (>) signs (Figure 1.7). The name, which you invent yourself, should describe the element's purpose and, in particular, its contents. An element is generally concluded with a closing tag, comprised of the same name preceded with a forward slash, enclosed in the familiar less than and greater than signs. The exception to this is called an empty element which may be "selfclosing,".
Elements may have attributes. Attributes, which are contained within an element's opening tag, have quotation-mark delimited values that further describe the purpose and content (if any) of the particular element (Figure 1.8). Information contained in an attribute is generally considered metadata; that is, information about the data in the element, as opposed to the data itself. An element can have as many attributes as desired, as long as each has a unique name.
About the Author
Kevin Howard Goldberg has been working with computers since 1976 when he taught himself BASIC on his elementary school's PDP 11/70. Since then, Kevin's career has included management consulting, lead software development and in his current capacity, he runs technology operations for a world-class Internet Strategy, Marketing and Development company. Kevin holds a bachelor's degree in Economics and Entrepreneurial Management from the Wharton School of Business at the University of Pennsylvania, and is a candidate for a master's degree in Computer Science at the University of California, Los Angeles.
Reader J. Hatch says, "This book is a great way to get started if you have not done a lot of HTML and are looking for a way to get your feet wet with XML and it's interactions with HTML. It does a great job of step-by-step leading you through most of the basic concepts used in XML. If you don't know much about XML and want to get started and need a quick reference for figuring out what is going on in an XML file, this is a great starting point. It is compact and the approach is to explain something minimally and then show a example. There are example files to download that the book references. To get the most out of this book, you will have to download the examples."
Learn more about XML: Visual Quickstart Guide at amazon.com
Learn more at amazon.com