Anatomy of an XML Document
The best way to explain how an XML document is composed is to present one. The following example shows an XML document you might use to describe two authors:
<?xml version="1.0" encoding="us-ascii"?>
<authors>
<person id="lear">
<name>Edward Lear</name>
<nationality>British</nationality>
</person>
<person id="asimov">
<name>Isaac Asimov</name>
<nationality>American</nationality>
</person>
<person id="mysteryperson"/>
</authors>The first line of the document is known as the XML declaration. This tells a processing application which version of XML you are using (the version indicator is mandatory) and which character encoding you have used for the document. In this example, the document is encoded in ASCII. (The significance of character encoding is covered later in this chapter.)
If the XML declaration is omitted, a processor will make certain assumptions about your document. In particular, it will expect it to be encoded in UTF-8, an encoding of the Unicode character set. However, it is best to use the XML declaration wherever possible, both to avoid confusion over the character encoding and to indicate to processors which version of XML you’re using.
Elements and Attributes
The second line of the example
begins an element, which has been named authors.
The contents of that element include everything between the right
angle bracket (>) in
<authors> and the left angle bracket
(<) in </authors>. The
actual syntactic constructs <authors> and
</authors> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access