Elements and Attributes

In a Nutshell:

  • Elements are the fundadmental building blocks from which XML documents are built.
    • Elements consist of tags, and very often, the data enclosed in them.
    • Some elements consist of just one tag, which is self-closing
    • Other elements contain data and/or other elements
  • Attributes are properties of elements that give information about a particular instance of an element.
    • Attributes are contained in the first tag of an element
    • Each attribute has a name, specific to the element it is part of, and a value.
Learn more in this section about...
Got it already? Check yourself...

Elements

Elements are the basic unit of XML documents. Think of them as the atoms from which the "chemistry" of XML is derived. Let's look at an example of a simple XML document:

<train number="353">
<conductor name="Sylvester Ardmore" empNum="3785221"/>

<engineer name="Eric Olsen" empNum="3993625"/>
<locomotive type="P42">26</locomotive>
<consist>
<car type="baggage">133765</car>
<car type="coach">100236</car>
<car type="coach">100389</car>
<car type="coach">100725</car>
<car type="lounge/snack">110853</car>
</consist>
</train>

This illustrates each of the types of elements:

  • Elements with no data, but with other elements inside them:
    <train>
    <consist>
  • Elements with text data inside them:
    <locomotive>
    <car>
  • Elements with no data ("empty" elements)
    <conductor>
    <engineer>

Note also that elements that are not empty (the ones with data or other elements inside them) have a closing tag. The closing tag has a forward slash right after the opening angle bracket </ and no attributes.

Ask yourself: Is there another logical possibility for what could be in an element? Answer: Yes, you could have an element containing but text data and other elements. These are called mixed elements are are skipped here for simplicity.
Learn more about elements in the XML Specifications...  

What's the Difference between Empty and Non-empty Elements?

An empty element is one that has no content - it may have attributes, but everything is contained in one tag. Here are some more examples:

  • Element with content:
    <soda>Mountain Dew</soda>
  • Empty element (two ways to write it):
    <soda name="Mountain Dew"></soda>
    <soda name="Mountain Dew"/>
    The second example is "self-closing": the forward-slash just before the closing angle bracket /> signals that the element is closed, without needing the closing tag </soda> shown in the first example.
Ask yourself: Is the flexibility illustrated in these examples more helpful, or more confusing? You're right. (Whatever you said!) But I hope you'll find it helpful.

Attributes

Attributes are properties of elements that are listed inside the element's main tag.

Where to put attributes:
  • Attributes are placed in the main tag, which is either the first tag:
    <car type="coach">100389</car>
    or the only tag:
    <conductor name="Sylvester Ardmore" empNum="3785221"/>
  • If an element has a closing tag, attributes cannot be repeated in the closing tag:
    <locomotive type="P42">26</locomotive>
What attributes look like:
  • Each attribute starts with its name:
    type="coach"
    name="Sylvester Ardmore"

    empNum="3785221"
    The name is a kind of metadata
  • After the name comes the equal-sign =
  • Each attribute has one (and only one) value. The value is always in quotes, which can be either single ' or double "
    number="353"
    empNum='3785221'
    name="Eric Olsen"
    The value is a kind of data.
When to Use Attributes:

Attribute values and the text content of elements are both example of data, as opposed to metadata. So you may wonder how to decide whether to put data in an attribute or an element's data.

There are no hard-and-fast rules, but here are some considerations:

  • If CSS (Cascading Style Sheets) is used to format an XML document's output, newer browsers will be able to display the data in an element just like an HTML Web page. But data in attributes cannot be shown this way: instead, using XSLT (eXtensible Stylesheet Language Transformations) would be necessary. CSS is more likely to be familiar to Web designers, and is somewhat less complex, so in general, data that is intended to be displayed should be in an element rather than an attribute.
  • Because attributes are part of tags, most people are more comfortable with short attribute values. Data in elements can comfortably be made longer.
  • When an element has many different types of data associated with it, it is less bulky to put the data in attributes - especially if the data itself isn't very long. That's because attributes don't need opening and closing tags for their values - just quotation marks around them.

 

Ask yourself: What might be some other considerations in deciding whether a data item should be coded as an attribute value or element value? Answer: There are lots of considerations, and no one correct answer!
Learn more about attributes in the XML specifications...
Check yourself!