Breadcrumbs

XML Attributes

Attributes are used to describe more information about a particular element or 'tag'. Attributes are all over the popular HTML. Some examples of these are:

Code: XHTML

<a href=""></a>
<img src="" />
<input type="text" />

In the above examples the attributes are href, src, and type. Hopefully by now you see what attributes are, but don't worry if you still don't understand it or would like to have a better description, I'll explain it.

What is an attribute?

As I stated earlier attributes are used to describe more information about an element. For example we look at the href of the anchor tag. The href attribute describes the anchor tag telling the browser that the value of href has some sort of relation to the data held within the anchor tag.

Usefulness of Attributes

Attributes are useful in XML when you need to have two values for one element. There is a simple way around this, just use two elements to hold the data. Attributes make it easier by providing a way to have that added information attached to the element, keeping things organized.

For example say we want to make a store inventory. Here we can do it without attributes:

Code: XML

<inventory>
    <item>
        <aisle>2</aisle>
        <name>Soap</name>
        <provider>Clorox</provider>
    </item>
</inventory>

In the above example it would go on with many <item> tags containing a lot of information on each product. Now we can minimize the code and make it a little easier to read with attributes:

Code: XML

<inventory>
    <item aisle="2" provider="Clorox">Soap</item>
</inventory>

As you see this minimizes code, is slightly easier to read, and takes advantage of attributes.

Quote Style

There are single and double quotes in the world of coding, and of course with XML attributes you have to learn a few things. When you have a value for an attribute that doesn't use quotes in it then you have the choice to use either single or double quotes. If the value for an attribute contains quotes itself make the quote holding the value single and the quotes in the attribute double:

Code: XML

<item aisle="2" provider='Clorox "Bleach"'>Soap</item>

You can also reverse it with single quotes for the value and double to hold it:

Code: XML

<item aisle="2" provider="Clorox 'Bleach'">Soap</item>

Problems with Attributes

Attributes have a few downsides to them which often make using child elements much easier and efficient. The choice I leave to you whether or not to use attributes, but often it is just easier to use a fair share of them. For example let's go back to our inventory code.

Code: XML

<item id="r2-675>
    <name>Soap</name>
    <provider>Clorox</provider>
    <status quantity="100">Good</status>
</item>

As you see I took advantage of attributes without over doing them. If you feel like the value for an attribute is data itself it is best to just use a child element to hold it. In the above example the <status> has the attribute quantity. This attribute would probably have been better of being its own element, although I was only providing an example.

Summary

Don't be discouraged to use attributes and they offer some good organizational methods for your data, even HTML takes advantage of them.

Tags: