XML technologies / XML / XML in a Nutshell / XML basics / Attributes
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
XML elements can have attributes. An attribute is a name-value pair attached to an element’s start-tag. The name and value are separated by an equals sign and optional whitespace. Values are enclosed in single or double quotes. For example, our element person has an attribute geboren with value 1912-06-23 and an attribute gestorben with value 1954-06-07:
<person geboren="1912-06-23" gestorben="1954-06-07">
Alan Turing
</person>The next element is identical to an XML parser. It merely uses single quotes instead of double quotes, adds spaces around equals signs, and reorders attributes.
<person gestorben = '1954-06-07' geboren = '1912-06-23'>
Alan Turing
</person>Whitespace around equals signs is purely a matter of taste. Single quotes help when a value contains double quotes. Attribute order is irrelevant.
The following example uses attributes to encode the record-like information in A more complex XML document describing a person.
<person>
<name vorname="Alan" nachname="Turing"/>
<beruf wert="Informatiker"/>
<beruf wert="Mathematiker"/>
<beruf wert="Kryptograph"/>
</person>Code example: Describing a person with attributes
This raises the much-debated question of when to use child elements or attributes. Some argue that attributes should hold metadata about an element, leaving the information itself to elements. Others note that data and metadata are not always clearly distinguishable. The answer probably depends on how the information will be used.
An element cannot have two attributes with the same name. This is fine for birth or death dates, but may fail for multiple occupations, names, or addresses. Attributes also have limited structure: a value is unstructured text. Even a hyphen-separated year, month, and day approaches the limit of sensible attribute structure. Elements are more flexible and extensible. Attributes can nevertheless be convenient, and designers of an XML vocabulary choose which to use.
Attributes also help in narrative documents, as the following example shows. The distinction may be clearer here: narrative text is character data inside elements, while attributes supply additional information such as sources, image URLs, hyperlinks, and birth and death dates. Alternatives still exist: footnote numbers could be attributes rather than character data.
<biographie xmlns:xlink="http://www.w3.org/1999/xlink/">
<absatz><bild src="http://www.turing.org.uk/turing/pi1/busgroup.jpg" breite="152" hoehe="345"/></absatz>
<absatz><person geboren='1912/06/23' gestorben='1954/06/07'><vorname>Alan</vorname> <nachname>Turing</nachname></person> war einer der ersten Menschen, der wirklich die Bezeichnung <betonung>Informatiker</betonung> verdiente. Seine Arbeiten auf diesem Gebiet können nicht alle aufgezählt werden, am bekanntesten sind der nach ihm benannte <betonung xlink:type="simple" xlink:href="http://cogsci.ucsd.edu/~asaygin/tt/ttest.html">Turing-Test</betonung> sowie die <betonung xlink:type="simple" xlink:href="http://mathworld.wolfram.com/TuringMachine.html">Turing-Maschine</betonung>.</absatz>
<absatz><nachname>Turing</nachname> war außerdem ein anerkannter <beruf>Mathematiker</beruf> und <beruf>Kryptograph</beruf>. Mit seiner Hilfe gelang es den Alliierten, die deutsche Enigma-Maschine zu entschlüsseln.<fußnote src="The Ultra Secret, F.W. Winterbotham, 1974">1</fußnote></absatz>
<absatz>Am <datum><tag>7.</tag> <monat>Juni</monat> <jahr>1954</jahr></datum> beging er Selbstmord, nachdem er wegen seiner Homosexualität verurteilt worden war und gezwungen wurde, weibliche Hormone einzunehmen.<fußnote src="Alan Turing: the Enigma, Andrew Hodges, 1983">2</fußnote></absatz>
</biographie>Code example: A narrative XML document using attributes
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top