1.3 How XML Expresses Information
XML expresses information using four basic componentstags, attributes, data elements, and hierarchy. Each of these components serves a unique purpose; each represents a different "dimension" of information. In order to illustrate these basic components, we will use a simple XML fragment from an application dealing with readings from colorimeters (devices that measure colors using tri-stimulus readings).
Data elements are represented in bold type in Listing 1.1. In XML, a data element equates to "data" as we have traditionally thought of it. If we simply extract the data elements, we get "0, 255, 255", which is meaningless unless you know what the data definitions are. XML adds context to data, thereby giving it meaning, by adding tags (represented in regular type in the listing). Tags describe what data elements are. Attributes (represented in italics in the listing) tell us something about or how to interpret data elements. Colorimeters can represent RGB tri-stimulus values in a variety of resolutions. If the reading had been taken with a resolution of 16 bits, for example, values of "0, 255, 255" would represent a very dark cyan, instead of pure cyan. So, we need the "resolution-8" attribute to correctly interpret the RGB reading values in Listing 1.1.
Listing 1.1 Simple XML Fragment
<colorimeter_reading> <RGB resolution=8> <red> 0 </red> <green> 255 </green> <blue> 255 </blue> </RGB> </colorimeter_reading>
Now we have data (data elements), we know what they are (tags), and we know how to interpret them (attributes). The final step is to determine how to string it all together, and that is where hierarchy comes in. So far, we have represented three dimensions of information explicitly. The last dimension, how everything relates, is implied spatially. This means that much of what we need to know is contained in how we order the components of XML information. In order to give data meaning, a complete context must be provided, not just the most immediate tag or attribute. For example, if we simply say "red-0", it will not mean much because we have not provided an adequate context. If we include all tags in the hierarchy leading up to the reading of "0", we achieve a more complete context: "-colorimeter_ reading--RGB--red- 0". Although we have a complete understanding of what the data element represents and its value, some ambiguity as to how to interpret the value still remains. The attribute "resolution58" belongs to the tag "-RGB-". Because "-RGB-" is a part of our context, any attribute belonging to it (or any attribute belonging to any tag in our context for that matter) applies. Now we know how to interpret the value of the data element as well. Related information is represented in the hierarchy as siblings at various levels; as a result, hierarchy tells us how data elements are related to each other.