XML Notes

Table of Contents

Section 5: XML Attributes

XML attributes are normally used to describe XML elements or to provide additional information. From HTML you can remember this construct: <img src="computer.gif">. In this HTML example src is an attribute to the img element. The src attribute provides additional information about the element. Attributes are always contained within the start tag of an element. Here are some examples:

HTML Examples:

<img src=”computer.gif”>
<a href=”demo.php”>

XML Examples:

<file type=”gif”></file>
<person id=”3344”/>

Usually, or most commonly, attributes are used to provide information that is not a part of the content of the XML document. Often attribute data is more important to the XML parser than to the reader. In the example above, the person id is a counter value that is irrelevant to the reader, but important to software that wants to manipulate the person element.

You may also note that the person tag fails to use a typical closing tag (i.e. </person>) but includes a / at the end of the opening person tag. No, an error was not made in this exam... that is another way to close an XML element! Some tags contain only attributes, thus have no need for the separation of the opening and closing tag; so the open/close tags are done all at once. To correctly close a person tag, the slash (/) must appear at the very end of the XML element just before the > symbol.

Correct:

<group id="4420"/>

Incorrect:

<group / id="4420">
or
</group id="4420">