Datasheet
<title>ASP.NET 2.0 Beta Preview</title>
<author>Bill Evjen</author>
<year></year>
</book>
</books>
In this example, books is the document element, book elements are children of books, and title, and
author are children of book. The book elements contain no data (just other elements), but title, and author
contain data. The following line in the second book element contains neither data nor other elements.
<year></year>
Empty elements are perfectly legal in XML. An empty year element can optionally be written this way
for conciseness:
<year/>
Unlike HTML, XML requires that start tags be accompanied by end tags; therefore, the following XML is
never legal:
<year>2003
Also unlike HTML, XML is case-sensitive. A <year> tag closed by a </ Year> tag is not legal because
the cases of the
Ys do not match.
Because XML permits elements to be nested within elements, the content of an XML document can be
viewed as a tree. By visualizing the document structure in a tree, you can clearly understand the parent-
child relationships among the document’s elements.
Attributes
XML allows you to attach additional information to elements by including attributes in the elements’
start tags. Attributes are name/value pairs. The following book element expresses year as an attribute
rather than as a child element:
<book year=”2003”>
<title>XSLT Programmers Reference</title>
<author>Michael Kay</author>
</book>
Attribute values must be enclosed in single or double quotation marks and may include spaces and
embedded quotation marks. (An attribute value delimited by single quotation marks can contain double
quotation marks and vice versa.) Attribute names are subject to the same restrictions as element names and
therefore can’t include spaces. The number of attributes an element can be decorated with is not limited.
When defining a document’s structure, it’s sometimes unclear — especially to XML
newcomers — whether a given item should be defined as an attribute or an element.
In general, attributes should be used to define out-of-band data and elements to
define data that is integral to the document. In the previous example, it probably
makes sense to define year as an element rather than an attribute because year
provides important information about the book in question.
6
Chapter 1
04_596772 ch01.qxd 12/13/05 11:17 PM Page 6