1. Technology

Elements in XML

The Basic Part of an XML Document


In a previous article I showed how to use attributes in your XML elements, but what exactly is an XML element? In a nutshell, elements are the building blocks of markup languages. In XHTML, any of the tags are elements, such as:

  • <body></body>
  • <p></p>
  • <strong></strong>
  • and so on

Remember that an XML tag and an XML element are somewhat different. A paragraph element in XHTML might look something like this:

 Text of the paragraph.

This paragraph is an entire XHTML element. It is made up of the following parts:

  • the delimiters: "<" and ">"
  • the generic identifier: the "p" enclosed in the two delimiters
  • the opening and closing tags: "<p>" and "</p>"
  • the element content: "Text of the paragraph."

For elements that don't have content, like the img tag in XHTML, you would include the opening and closing tag in the same two delimiters:

<img src="shasta.gif" />

The great thing about XML is that you can create any element that you need for your document. In order to have a valid XML document, the elements must be described in an internal or external DTD or schema, but for the purposes of this article, we'll just concentrate on structuring elements in a document to keep it well-formed.

When you create a document, you can create elements within it both with and without content. And elements with content can contain other elements. For example, I might want to create an XML document that lists all my pets. The first element I would have is the container element:


In a DTD, the container element is created like this:

<!DOCTYPE mypets [
 ... elements contained within this container ...

The container element is the first and last element in the entire document. It will always contain other elements. In this case, I would need pet information elements such as:

  • <dog />
  • <cat />
  • <rabbit />
  • <horse />
  • <husband /> (KIDDING!)

For these elements, you might have a DTD declaration that looked something like this:

<!DOCTYPE mypets [
 <!ELEMENT rabbit EMPTY >
 <!ELEMENT horse EMPTY >

All of the elements I've suggested above have no content, but I could create them as elements with content, and then perhaps include more elements or attributes that indicate age, color, weight, or just a text description of that animal.

You can create an element that contains anything at all, but this isn't a good idea as it doesn't give you any control over what's in your XML document:


A more common element is one that contains mixed data or PCDATA. This allows your element to have 0 or more characters, but can contain any XML compliant parsed character data.


Elements can also contain other elements as well as character data. The other elements are listed after the PCDATA and indicate that the tag can contain them in any order (or not at all):

<!ELEMENT horse (#PCDATA) | size | color | own >

If you want to specify the exact elements that are contained in another element you would separate them in a comma-delimited list in your DTD in the sequence you want them to be in:

<!ELEMENT rabbit (age, gender) >

So, if I were going to create my entire pets document, I might have a combination of different types of elements. They would all be contained in the root element (<mypets>).

 <cat />

Elements are the key to creating an XML document.

Previous Features

©2014 About.com. All rights reserved.