An attribute is placed within the opening tag of the element. The attributes in XML are very similar to those in HTML except those in XML are required to have values. Examples of both incorrect and correct ways to insert attributes are shown in the examples below.
Both double quote (“) and single quote (‘) are valid in XML documents and it is up to the creator to determine which ones to use. There are a few instances where one type of quotation may make more sense than the other. For example, if the attribute value has its own set of double quotes, it may make more sense to use a single quote to close the attribute.
When using attributes, there are a few drawbacks that you should keep in mind. Attributes cannot do the following:
- Contain multiple values like elements
- Contain tree structures like elements
- Easily be expanded for future changes
In addition to these drawbacks, attributes can sometimes be difficult to maintain and read. It is a good practice to use elements to present data and attributes to present data that is not relevant to data. Also, if using metadata, it can be stored as attributes rather than elements.