BEGINNING XML PART 2 – ATTRIBUTES (Page 2)

How It Works

Using attributes, we added some information about the CD’s serial number and length to our document:

<CD serial=B6B41B
  disc-length='36:55'>

When the XML parser got to the “=” character after the serial attribute, it expected an opening quotation mark, but instead it found a B. This is an error, and it caused the parser to stop and raise the error to the user.

So we changed our serial attribute declaration:

<CD serial='B6B41B'

and this time the browser displayed our XML correctly.

The information we added might be useful, for example, in the CD Player application we considered earlier. We could write our CD Player to use the serial number of a CD to load any previous settings the user may have previously saved (such as a custom play list).

Why Use Attributes?

There have been many debates in the XML community about whether attributes are really necessary, and if so, where they should be used. Here are some of the main points in that debate:

Attributes Can Provide Metadata that May Not be Relevant to Most Applications Dealing with Our XML

For example, if we know that some applications may care about a CD’s serial number, but most won’t, it may make sense to make it an attribute. This logically separates the data most applications will need from the data that most applications won’t need.

In reality, there is no such thing as “pure metadata” – all information is “data” to some application. Think about HTML; you could break the information in HTML into two types of data: the data to be shown to a human, and the data to be used by the web browser to format the human-readable data. From one standpoint, the data used to format the data would be metadata, but to the browser or the person writing the HTML, the metadata is the data. Therefore, attributes can make sense when we’re separating one type of information from another.

What Do Attributes Buy Me that Elements Don’t?

Can’t elements do anything attributes can do?

In other words, on the face of it there’s really no difference between:

<name nickname='Shiny John'></name>

and:

<name>
 <nickname>Shiny John</nickname>
</name>

So why bother to pollute the language with two ways of doing the same thing?

The main reason that XML was invented was that SGML could do some great things, but it was too massively difficult to use without a fully-fledged SGML expert on hand. So one concept behind XML is a simpler, kinder, gentler SGML. For this reason, many people don’t like attributes, because they add a complexity to the language that they feel isn’t needed.

On the other hand, some people find attributes easier to use – for example, they don’t require nesting and you don’t have to worry about crossed tags.

Why Use Elements, if Attributes Take Up So Much Less Space?

Wouldn’t it save bandwidth to use attributes instead?

For example, if we were to rewrite our <name> document to use only attributes, it might look like this:

<name nickname='Shiny John' first='John' 
middle='Fitzgerald Johansen' last='Doe'></name>

Which takes up much less space than our earlier code using elements.

However, in systems where size is really an issue, it turns out that simple compression techniques would work much better than trying to optimize the XML. And because of the way compression works, you end up with almost the same file sizes regardless of whether attributes or elements are used.

Besides, when you try to optimize XML this way, you lose many of the benefits XML offers, such as readability and descriptive tag names. And there are cases where using elements allows more flexibility and scope for extension. For example, if we decided that first needed additional metadata in the future, it would be much simpler to modify our code if we’d used elements rather than attributes.

Why Use Attributes when Elements Look So Much Better? I Mean, Why Use Elements when Attributes Look So Much Better?

Many people have different opinions as to whether attributes or child elements “look better”. In this case, it comes down to a matter of personal preference and style.

In fact, much of the attributes versus elements debate comes from personal preference. Many, but not all, of the arguments boil down to “I like the one better than the other”. But since XML has both elements and attributes, and neither one is going to go away, you’re free to use both. Choose whichever works best for your application, whichever looks better to you, or whichever you’re most comfortable with.