Home > Articles > Web Services > XML

  • Print
  • + Share This

Why the End Tag?

At first, the need to terminate each element with an end tag is annoying. It is required because XML does not have predefined elements.

An HTML browser can work out when an element has no closing tags because it knows the structure of the document, it knows which elements are allowed where and it can deduce where each element should end.

Indeed, if the following is an HTML fragment, a browser does not need end tags for paragraphs, nor does it need an empty tag for the break, see Listing 1.

Listing 1: A HTML document needs no end tags

<P><B>John Doe</B>
<P>34 Fountain Square Plaza<BR>
Cincinnati, OH 45202<BR>
US
<P>Tel: 513-744-8889
<P>Tel: 513-744-7098
<P>Email: jdoe@emailaholic.com

The browser can deduce where the paragraphs end because it knows that paragraphs cannot nest. Therefore the beginning of a new paragraph must coincide with the end of the previous one. Likewise, the browser knows that the break is an empty element. Because of all this a priori knowledge, the browser can "fill in the blank" and knows the document must be interpreted as:

<P><B>John Doe</B></P>
<P>34 Fountain Square Plaza<BR></BR>
Cincinnati, OH 45202<BR></BR>
US</P>
<P>Tel: 513-744-8889</P>
<P>Tel: 513-744-7098</P>
<P>Email: jdoe@emailaholic.com</P>

However, an XML processor does not know the structure of the document because you define your own tags. So, an XML processor does not know that p elements (it does not know they are paragraphs, either) cannot nest. If Listing 1 was XML, the processor could interpret it as:

<P><B>John Doe</B>
  <P>34 Fountain Square Plaza
    <BR>Cincinnati, OH 45202</BR>
    <BR>US</BR>
    <P>Tel: 513-744-8889
      <P>Tel: 513-744-7098
        <P>Email: jdoe@emailaholic.com</P>
      </P>
    </P>
  </P>
</P>

or as:

<P><B>John Doe</B></P>
<P>34 Fountain Square Plaza
  <BR>Cincinnati, OH 45202</BR>
  <BR/>US
  <P>Tel: 513-744-8889</P>
  <P>Tel: 513-744-7098</P>
</P>
<P>Email: jdoe@emailaholic.com</P>

There are many other possibilities and that's precisely the problem. The processor wouldn't know which one to pick so the markup has to be unambiguous.

TIP

When declaring the structure of documents with DTDs, the XML processor could use the DTD to resolve ambiguities in the markup. Indeed, that's how SGML processors work. However be aware that certain XML processors ignore DTDs.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.