Creating Documents  «Prev 

Importance of starting and ending Tags in XML

In HTML, you became familiar with the fact that you could use the following syntax to create a bulleted list:

<UL>
<LI>Bullet list item one
<LI>Bullet list item two
<LI>Bullet list item three
</UL>

Note that the <UL> and </UL> tags denote the beginning and ending of an unordered list. <LI> denotes the beginning of the current list item, but also can be inferred to denote the end of the previous list item in its second and third appearance. In other words, the end of the element has been inferred. If you were to code like this in an XML document, however, the browser would be instructed not to render this page because it is not well-formed.

In XML, tags will never be inferred and must always be made explicit. In other words, if the above example were to be included in an XML document, it would have to be written like this:

<UL>
<LI>Bullet list item one</LI>
<LI>Bullet list item two</LI>
<LI>Bullet list item three</LI>
</UL>  

In the second instance of this list, you see that each list item is enclosed between beginning and ending tags, meeting one criterion for a well-formed XML document.

Listing 3-1. Hello, XML

<encounter>
<greeting>Hello, XML!</greeting>
<response>Hello, what can I do for you?</response>
</encounter>

This XML document contains three elements: encounter, greeting, and response. Here is the greeting element:
<greeting>Hello, XML!</greeting>
An element consists of a start tag, the element’s content (which can be empty), and an end tag. A start tag minimally consists of the “<” character, a tag name, and the “>” character. The tag name can be followed by attribute declarations. An end tag consists of the character sequence “</”, followed immediately by the tag name and the closing bracket. (See Figure 3-1.)
Helloworld: Element and tags
Figure 3-6 An element and its tags