2.3. AttributesXML elements can have attributes. An attribute is a name-value pair attached to the element's start-tag. Names are separated from values by an equals sign and optional whitespace. Values are enclosed in single or double quotation marks. For example, this person element has a born attribute with the value 1912-06-23 and a died attribute with the value 1954-06-07: <person born="1912-06-23" died="1954-06-07"> Alan Turing </person> This next element is exactly the same as far an XML parser is concerned. It simply uses single quotes instead of double quotes, puts some extra whitespace around the equals signs, and reorders the attributes. <person died = '1954-06-07' born = '1912-06-23' > Alan Turing </person> The whitespace around the equals signs is purely a matter of personal aesthetics. The single quotes may be useful in cases where the attribute value itself contains a double quote. Attribute order is not significant. Example 2-4 shows how attributes might be used to encode much of the same information given in the data-oriented document of Example 2-2. Example 2-4. An XML document that describes a person using attributes<person> <name first="Alan" last="Turing"/> <profession value="computer scientist"/> <profession value="mathematician"/> <profession value="cryptographer"/> </person> This raises the question of when and whether one should use child elements or attributes to hold information. This is a subject of heated debate. Some informaticians maintain that attributes are for metadata about the element while elements are for the information itself. Others point out that it's not always so obvious what's data and what's metadata. Indeed, the answer may depend on where the information is put to use. What's undisputed is that each element may have no more than one attribute with a given name. That's unlikely to be a problem for a birth date or a death date; it would be an issue for a profession, name, address, or anything else of which an element might plausibly have more than one. Furthermore, attributes are quite limited in structure. The value of the attribute is simply a text string. The division of a date into a year, month, and day with hyphens in the previous example is at the limits of the substructure that can reasonably be encoded in an attribute. Consequently, an element-based structure is a lot more flexible and extensible. Nonetheless, attributes are certainly more convenient in some applications. Ultimately, if you're designing your own XML vocabulary, it's up to you to decide when to use which. Attributes are also useful in narrative documents, as Example 2-5 demonstrates. Here it's perhaps a little more obvious what belongs to elements and what to attributes. The raw text of the narrative is presented as character data inside elements. Additional information annotating that data is presented as attributes. This includes source references, image URLs, hyperlinks, and birth and death dates. Even here, however, there's more than one way to do it. For instance, the footnote numbers could be attributes of the footnote element rather than character data. Example 2-5. A narrative XML document that uses attributes<biography xmlns:xlink="http://www.w3.org/1999/xlink/namespace/"> <image source="http://www.turing.org.uk/turing/pi1/bus.jpg" width="152" height="345"/> <person born='1912-06-23' died='1954-06-07'><first_name>Alan</first_name> <last_name>Turing</last_name> </person> was one of the first people to truly deserve the name <emphasize>computer scientist</emphasize>. Although his contributions to the field were too numerous to list, his best-known are the eponymous <emphasize xlink:type="simple" xlink:href="http://cogsci.ucsd.edu/~asaygin/tt/ttest.html">Turing Test</emphasize> and <emphasize xlink:type="simple" xlink:href="http://mathworld.wolfram.com/TuringMachine.html">Turing Machine</emphasize>. <last_name>Turing</last_name> was also an accomplished <profession>mathematician</profession> and <profession>cryptographer</profession>. His assistance was crucial in helping the Allies decode the German Enigma machine.<footnote source="The Ultra Secret, F.W. Winterbotham, 1974">1</footnote> He committed suicide on <date><month>June</month> <day>7</day>, <year>1954</year></date> after being convicted of homosexuality and forced to take female hormone injections.<footnote source="Alan Turing: the Enigma, Andrew Hodges, 1983">2</footnote> </biography> Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|