Elements are the building blocks of XML, dividing a document into a hierarchy of regions, each serving a specific purpose. Some elements are containers, holding text or elements. Others are empty, marking a place for some special processing such as importing a media object. In this section, I’ll describe the rules for how to construct elements.


Figure 2-9 shows the syntax for a container element. It begins with a start tag consisting of an angle bracket (1) followed by a name (2). The start tag may contain some attributes (3) separated by whitespace, and it ends with a closing angle bracket (4). After the start tag is the element’s content and then an end tag. The end tag consists of an opening angle bracket and a slash (5), the element’s name again (2), and a closing bracket (4). The name in the end tag must match the one in the start tag exactly.

Container element syntax
Figure 2-9. Container element syntax

An empty element is very similar, as seen in Figure 2-10. It starts with an angle bracket delimiter (1), and contains a name (2) and a number of attributes (3). It is closed with a slash and a closing angle bracket (4). It has no content, so there is no need for an end tag.

Empty element syntax
Figure 2-10. Empty element syntax

An attribute defines a property of the element. It associates a name with a value, which ...

Get Learning XML, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.