SCDJWS Study Guide: XML Basic
Printer-friendly version |
Mail this to a friend
XML Attributes
XML elements can have attributes which provide a mechanism to further define or classify XML elements; the attributes must be defined in the start tag (opening tag) and never the end tag of XML elements. It is a Name-value pair associated with an element. Attribute names can be any string, but cannot be repeated within the same element. Attribute values are always quoted by either single or double quotes.
The attributes are typically used to describe the content of the XML element.
- Attributes provide addition information about elements. In the following example, the "src" attribute provides additional information about the "img" element:
<img src="logo.gif"/>
- Attributes provide information that is not part of the data contained by the element. In the following example, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
<file type="gif">computer.gif</file>
A document that obeys the "nested tags" rule and does not repeat an attribute within a tag is said to be well-formed.
Rules applied to XML attributes
Attribute names are case sensitive, can't start with "xml". When naming XML attributes, it follows the same rule of naming XML elements.
The same attribute name CAN NOT appear more than once on an element, but an element CAN HAVE more than one attributes associate with it.
Attribute MUST be declared in the start tag and never the end tag of an element.
Attributes MUST have values even if that value is just any empty string "".
Attribute value MUST be in quotes. Either single or double quotes can be used, but they have to match. Double quotes are more common, but sometimes if the attribute value itself contains quotes, it is necessary to use single quotes. Example:
<!-- correct -->
<movie genre="Family and Children">
<!-- correct -->
<movie genre='Family and Children'>
<!-- incorrect -->
<movie genre="Family and Children'>
Problems using attributes
attributes cannot contain multiple values (child elements can)
attributes are not easily expandable (for future changes)
attributes cannot describe structures (child elements can)
attributes are more difficult to manipulate by program code
attribute values are not easy to test against a DTD
Use of elements vs. Attributes
You can store data in child elements or in attributes. Use of elements vs. Attributes is considered a matter of style. There are no rules about when to use attributes, and when to use nested elements. A common use for attributes is to express dimension or type.
Take a look the following examples, they provide the same information:
<!-- example 1 -->
<movie genre='Horror, Science Fiction, Fantasy, Action and Adventure'>
<title>King Kong</title>
<theatrical_released>03/02/1950</theatrical_released>
</movie>
<!-- example 2 -->
<movie>
<genre>Horror, Science Fiction, Fantasy, Action and Adventure</genre>
<title>King Kong</title>
<theatrical_released>03/02/1950</theatrical_released>
</movie>
Comparison:
| Element | Attribute |
|---|---|
| Contents is noun(s) | Content (value) is an adjective |
| Describes structure | Describes quality or characteristic |
| Any content | May have discrete set of values |
| May contain other elements/structures | No pieces or sub-parts |
| "Data" | "Metadata" ("about" the data) |
| Text("about" the data) | Links, pointers, formatting specs |
Pro & Cons
| Element | Attribute | |
|---|---|---|
| Pro |
|
|
| Con |
|
|
Recommendation:
Use elements for data that will be produced or consumed by a business application, and attributes for metadata.