Attributes
XML 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 as 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 record-like document of Example 2-2.
<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 ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.