Home arrow XML arrow Page 9 - XML Basics (part 1)

To Attribute Or Not To Attribute... - XML

Struggling with XML? Can't figure out the difference between an entity and a namespace? Fear not - our XML series has all the answers. This introductory article discusses the origins and design goals of XML, the basic rules of XML markup, and how to use elements and attributes in an XML document.

  1. XML Basics (part 1)
  2. A Little History
  3. The Big Picture
  4. The Hammer And The Chisel
  5. Lights, Camera, Action!
  6. Breaking It Down
  7. Simply Element-ary
  8. Anyone For Chicken?
  9. To Attribute Or Not To Attribute...
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 4
July 23, 2001

print this article


Elements can also contain attributes, which provide additional information about the element. Attributes are name-value pairs which appear within the start tag of an element and can be used to provide additional descriptive parameters or default values to the element. For example, the following XML snippet uses the attribute "sex" to provide additional data on the <person> element:

<cast> <person sex="male">Hugh Jackman</person> <person sex="male">Patrick Stewart</person> <person sex="male">Ian McKellen</person> <person sex="female">Famke Janssen</person> </cast>
Attributes must always appear after the element name, and attribute names are case-sensitive. Attribute values must always be enclosed within quotation marks, and the same attribute should not be repeated twice within the same element. If your document is linked to a DTD, you can enforce rules on the types of values an attribute may and may not accept.

It should be noted that the line between attributes and elements is often very fine, since the two perform similar functions. For example, while it is perfectly valid for me to describe a

<person sex="male">Hugh Jackman</person>
I could achieve exactly the same effect by breaking the data down and assigning it to a series of elements.

<person> <name>Hugh Jackman</name> <sex>male</sex> </person>
In other words - the decision as to whether to use an attribute or an element can sometimes be a tricky one, and needs to be made on a case-by-case basis. Most experts seem to agree that this is an implementation decision, and must be made keeping in mind the purpose for which the document is going to be used.

Valid reasons for using attributes over elements would include assigning an ID to a specific element,

<review id="6548450">...</review>
or describing characteristics of the element itself.

The <animal color="red">wolf</animal> jumped over the <vegetable color="blue">aubergine</vegetable>
If you need to restrict attribute values to some pre-defined options, you can use a DTD to specify a list of allowed and default values, thereby cutting down on the possibility of errors and incompatible data.

If you're interested in a detailed discussion and debate of this issue, you should make it a point to visit http://xml.coverpages.org/elementsAndAttrs.html, which has some interesting comments and opinions by experts in the field on this very topics.

That's about it for the moment. In the next article, I'm going to continue this discussion of basic XML concepts with a look at entities, namespaces, and processing instructions - so make sure that you don't miss that one. Until then...stay healthy!

This article copyright Melonfire 2001. All rights reserved.

>>> More XML Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates


Dev Shed Tutorial Topics: