- XML Elements
- Generic Identifiers
- Some Rules for Naming Elements
- Storing the Data in XML
- Parsed Character Data
- Bypassing Parsing with CDATA
- Attributes
- When to Use Attributes
- Classifying Attributes: Attribute Types
- Attribute Rules
- Well-Formedness Rules
- Creating a Well-Formed XML Document
- The Basics of Validation
- How Do Applications Use XML?
- An Overview of XML Tools
- Roadmap
- Additional Resources
When to Use Attributes
It would be possible to create an entire document using nothing but elements. After all, you can use elements to mark up any kind of data. For example, we could describe a camera using only elements:
<camera> <manufacturer>Nikon</ manufacturer> <model>D1</model> <lens>28mm</lens> <format>35mm</format> </camera>
There is nothing wrong with this approach. In fact, often elements are more flexible than attributes, and can be easier to alter. However, we could also structure this XML using attributes:
<camera manufacturer="Nikon" model="D1" lens="28mm" format="35mm"> </camera>
With an example of this level of simplicity, there isn't really a huge impact in the differences between using elements versus attributes. However, some types of information naturally have a clearer structure. For example, look at this description of a family:
<family> <parent>John Doe</parent> <parent>Jane Doe</parent> <son>Timmy Doe</son> <daughter>Sally Doe</daughter> </family>
The grouping of the family members within a family element makes some structural sense, but we could take it even further:
<family> <parents> <father>John Doe</father> <mother>Jane Doe</mother> </parents> <children> <son>Timmy Doe</son> <daughter>Sally Doe</daughter> </children> </family>
This makes the relationships a little clearer, but we are still element centric. Now, what if we wanted to add some information about each family member, such as age? One way might be to add elements:
<family> <parents> <father> John Doe <age>39</age> </father> <mother>Jane Doe</mother> </parents> <children> <son>Timmy Doe</son> <daughter>Sally Doe</daughter> </children> </family>
However, this clutters our tree structure somewhat. We might instead use an age attribute:
<family> <parents> <father age="39">John Doe</father> <mother age="37">Jane Doe</mother> </parents> <children> <son age="10">Timmy Doe</son> <daughter age="8">Sally Doe</daughter> </children> </family>
Now, we have the age information associated with each element, but without adding unnecessary elements, and keeping the information streamlined.
There really are no right and wrong answers when it comes to choosing when to use elements versus attributes. The best course of action is to take a close look at the data you are describing and to see whether you spot a natural breakdown in the data itself. Barring that, it is mostly a matter of personal preference.