SCDJWS Study Guide: XML Basic


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

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

  1. Attribute names are case sensitive, can't start with "xml". When naming XML attributes, it follows the same rule of naming XML elements.

  2. 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.

  3. Attribute MUST be declared in the start tag and never the end tag of an element.

  4. Attributes MUST have values even if that value is just any empty string "".

  5. 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:

ElementAttribute
Contents is noun(s)Content (value) is an adjective
Describes structureDescribes quality or characteristic
Any contentMay have discrete set of values
May contain other elements/structuresNo pieces or sub-parts
"Data""Metadata" ("about" the data)
Text("about" the data)Links, pointers, formatting specs


Pro & Cons

 ElementAttribute
Pro
  • Can contain other elements.
  • Are more extensible, for instance, attributes can be added without affecting parsing application.
  • Can be repeated.
  • Are more readable, when the values are lengthy.
  • Can be added to the instance by specifying default values. Elements cannot (they must appear to receive a default value).
  • Are atomic and cannot be extended and its existence should serve to remove any and all possible ambiguity of the element it describes.
  • Are less verbose.
Con
  • Require start and end tags, so are therefore more verbose.
  • May not be extended by adding children or other attributes

Recommendation:

Use elements for data that will be produced or consumed by a business application, and attributes for metadata.



Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |