1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

XML 101
Lesson 4
XML Elements

 Class Resources
• Part 1: Parts of an XML Document
• Part 2: XML Elements

• XML Class Index
 Get More Help
If you need more help with XML - post your question to the Web Design forum. Other students, class mentors, and I all review that board regularly to help you out with the class.

• Post Your Question Now
• How to Post

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>
  • <b></b>
  • 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, like the img tag in XHTML, that don't have content, 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 >

All of the elements I've suggested above have no content, but I could create them as elements with content, and then perhaps include tags 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: <!ELEMENT dog ANY >

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 a great XML document.

First Page: Anatomy of an XML Document > 1, 2

©2016 About.com. All rights reserved.