XML - Attributes


Advertisements

This chapter describes the XML attributes. Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.

Syntax

An XML attribute has the following syntax −

<element-name attribute1 attribute2 >
....content..
< /element-name>

where attribute1 and attribute2 has the following form −

name = "value"

value has to be in double (" ") or single (' ') quotes. Here, attribute1 and attribute2 are unique attribute labels.

Attributes are used to add a unique label to an element, place the label in a category, add a Boolean flag, or otherwise associate it with some string of data. Following example demonstrates the use of attributes −

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE garden [
   <!ELEMENT garden (plants)*>
   <!ELEMENT plants (#PCDATA)>
   <!ATTLIST plants category CDATA #REQUIRED>
]>

<garden>
   <plants category = "flowers" />
   <plants category = "shrubs">
   </plants>
</garden>

Attributes are used to distinguish among elements of the same name, when you do not want to create a new element for every situation. Hence, the use of an attribute can add a little more detail in differentiating two or more similar elements.

In the above example, we have categorized the plants by including attribute category and assigning different values to each of the elements. Hence, we have two categories of plants, one flowers and other shrubs. Thus, we have two plant elements with different attributes.

You can also observe that we have declared this attribute at the beginning of XML.

Attribute Types

Following table lists the type of attributes −

Attribute Type Description
StringType It takes any literal string as a value. CDATA is a StringType. CDATA is character data. This means, any string of non-markup characters is a legal part of the attribute.
TokenizedType

This is a more constrained type. The validity constraints noted in the grammar are applied after the attribute value is normalized. The TokenizedType attributes are given as −

  • ID − It is used to specify the element as unique.

  • IDREF − It is used to reference an ID that has been named for another element.

  • IDREFS − It is used to reference all IDs of an element.

  • ENTITY − It indicates that the attribute will represent an external entity in the document.

  • ENTITIES − It indicates that the attribute will represent external entities in the document.

  • NMTOKEN − It is similar to CDATA with restrictions on what data can be part of the attribute.

  • NMTOKENS − It is similar to CDATA with restrictions on what data can be part of the attribute.

EnumeratedType

This has a list of predefined values in its declaration. out of which, it must assign one value. There are two types of enumerated attribute −

  • NotationType − It declares that an element will be referenced to a NOTATION declared somewhere else in the XML document.

  • Enumeration − Enumeration allows you to define a specific list of values that the attribute value must match.

Element Attribute Rules

Following are the rules that need to be followed for attributes −

  • An attribute name must not appear more than once in the same start-tag or empty-element tag.

  • An attribute must be declared in the Document Type Definition (DTD) using an Attribute-List Declaration.

  • Attribute values must not contain direct or indirect entity references to external entities.

  • The replacement text of any entity referred to directly or indirectly in an attribute value must not contain a less than sign (<)

Advertisements