Return type:
The value of the specified attribute.
attribute attribute-name (of element-expression)?  indexer


You can access the value of an XML or SGML attribute using the attribute operator.

Attribute references are always treated as string expressions, even if the attribute was declared in the SGML document to be of type NUMBER. However, string expressions which contain a valid representation of a decimal number can be used anywhere where a numeric expression is permitted, so this interpretation places no restriction on the use of attributes.

Attributes can always be further identified by following the attribute name with an element qualifier.

Within the external-data-entity rule, unqualified attributes are data attributes; in other contexts, they are attributes of the current element.

An item-based indexer can be used if the attribute was declared as a list-valued attribute. However, unlike shelves, for which the lastmost value is selected unless indicated otherwise, attributes do not have a default selected value: if no indexer is specified, the whole attribute value is either tested or is output as a single unit.

Attribute references provide the attribute value unmodified, except for:

  • transformations specified by ISO 8879, the SGML standard, such as uppercasing of NAME values, and normalization of non-CDATA values, and
  • specifying the use of a different delimiter string by a delimiter declaration (which is, by default, a single space) to separate the tokens of a list-value attribute.

In particular, translate rule processing is not performed on the value of an attribute when it is referenced using the attribute herald. This contrasts with references to an attribute value using the "%v" modifier.

Attempting to access an attribute will cause an error:

  • if the element-expression specifies an element that does not exist, or
  • if the start tag for the specified element or the entity declaration for the external entity does not provide an explicit value for the attribute and the attribute is declared as #implied, #required or #current (and no previous occurrence of an element named in the same declaration gave the attribute a value).

When using an element-expression the programmer should be aware that:

  • the of element qualifier is usually not needed, since, in most contexts, unqualified attributes are assumed to be attributes of the current element. In external-data-entity rules, however, attributes by default are assumed to be data attributes. In that context, the of element qualifier is significant.
  • the of ancestor, of preparent and of open element qualifiers refer to the specified ancestor that was most recently opened (the one the fewest "generations" removed from the current element).
  • the of ancestor, of preparent and of open element qualifiers can list several element names.

In the header and body of an external-data-entity rule, all unqualified references to attributes actually refer to data attributes of the external entity being processed. In all other rules, they refer to element attributes (attributes of element start tags).

Unqualified references to attributes inside functions always refer to element attributes. In order to refer to the data attributes of an external entity being processed, the qualifier of entity must be specified.

Element qualifiers can themselves be qualified.

This sample shows how the of ancestor qualifier can list several element names. Note that of preparent and of open element can also be used to perform this function.

  attribute "indent" of ancestor ("numlist" | "bullist" | "deflist")

Specifically, this line of code refers to the indent attribute of a containing numlist, bullist, or deflist, whichever comes first, searching outwards from the current element. Note that an error would result if the first element encountered did not have a value specified for the indent attribute.

The following example illustrates how element qualifiers can be used with attribute references. By using the element qualifier of ancestor, this line of code refers to the date attribute of an enclosing element whose name is change.

  attribute "date" of ancestor "change"