Go to previous pageGo to next page

1.3.4. XML Attributes

XML elements can have attributes in the start tag. They are used to provide additional information about elements.

Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:

XML AttributeXML Attributeimportant

Attributes values must always be enclosed in quotes, but either single or double quotes can be used:

XML Attribute with double quotesXML Attribute with double quotesXML Attribute with single quotesXML Attribute with single quotes

Use of elements versus attributes

Data can be stored in child elements or in attributes.

Information stored as attributeInformation stored as attributeInformation stored as elementInformation stored as element

In the first example the sex is an attribute. In the last, sex is a child element. Both examples provide the same information.

important

There are no strict rules whether to use attributes or child elements. It is recommended though to use elements to describe data itself and attributes only for metadata (information that describes the data).

Exercise

Imagine you want to store the data of a map in an XML document. Which elements have to be included in this document and how should the structure of these elements look like?
Possible elements could be: map scale, width and height of the map, projection, legend, title, subtitle, relief and other map layers, etc.
Create an XML document that includes all elements that are needed for a map. Which elements are included as attributes and which ones as elements? Hand in your XML document to your tutor.



Go to previous page
Go to next page