Library sample chapters

Beginning XML

Empy Elements

Sometimes an element has no data. Recall our earlier example, where the middle element contained no name:

  <name nickname='Shiny John'>
    <first>John</first>
    <!--John lost his middle name in a fire-->
    <middle></middle>
    <last>Doe</last>
  </name>

In this case, you also have the option of writing this element using the special empty element syntax:

  <middle/>

This is the one case where a start-tag doesn't need a separate end-tag, because they are both combined together into this one tag. In all other cases, they do.

Recall from our discussion of element names that the only place we can have a space within the tag is before the closing ">". This rule is slightly different when it comes to empty elements. The "/" and ">" characters always have to be together, so you can create an empty element like this:

  <middle />

but not like these:

  <middle/ >
  <middle / >

Empty elements really don't buy you anything - except that they take less typing - so you can use them, or not, at your discretion. Keep in mind, however, that as far as XML is concerned <middle></middle> is exactly the same as <middle/>; for this reason, XML parsers will sometimes change your XML from one form to the other. You should never count on your empty elements being in one form or the other, but since they're syntactically exactly the same, it doesn't matter. (This is the reason that IE5 felt free to change our earlier <parody></parody> syntax to just <parody/>.

Interestingly, nobody in the XML community seems to mind the empty element syntax, even though it doesn't add anything to the language. This is especially interesting considering the passionate debates that have taken place on whether attributes are really necessary.

One place where empty elements are very often used is for elements that have no (or optional) PCDATA, but instead have all of their information stored in attributes. So if we rewrote our <name> example without child elements, instead of a start-tag and end-tag we would probably use an empty element, like this:

  <name first="John" middle="Fitzgerald Johansen" last="Doe"/>

Another common example is the case where just the element name is enough; for example, the HTML <BR> tag might be converted to an XML empty element, such as the XHTML <br/> tag. (XHTML is the latest "XML-compliant" version of HTML.)

Comments

  1. 16 Dec 2006 at 12:47

    Hi there

    I firstly want to make it clear I am a total beginner when it comes to XHTML etc.
    I am trying to write a number of pages based on some templates and have problems with CSS rendering.

    For my CSS to render properly I have figured out I need the statement <?xml version="1.0" encoding="utf-8"?> in the first line of my php page. So far so good.

    But for my php page to be able to include some statements from the database, I also need <?session_start();?> to be on the first line of my php page.

    Whatever I try, I cannot figure out how to fix the problem.

    Any ideas?

    Cheers

    John

  2. 14 May 2006 at 10:04

    it is very good and useful thanks

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Beginning XML.

Leave a comment

Sign in or Join us (it's free).

AddThis

Events coming up

  • Jul 13

    SmartClient

    California, United States

    A Smart Client is an application that uses local processing, consumes XML Web Services and can be deployed and updated from a centralized server. While the .NET Framework (Windows Forms) and the .

Want to stay in touch with what's going on? Follow us on twitter!