Provide a set of attributes that can be placed on all, or most, elements in the document type.
There are properties that belong to every element in the document. These properties need to be presented to the document author in an understandable way.
This is a widely applicable pattern and can be used anywhere that a large number of elements need the same attributes applied. It is a common technique for attributes such as IDs, security, and roles.
The usability of the document type depends a lot on consistency, and this pattern can be used to add consistency.
Create a common set of attributes that will be used by all or most of the elements in the document type.
Common Attributes in DTDs
This examples shows a DTD that has two common attributes,
role, on its all of its elements. The common attribute definition is placed in a parameter entity which is referenced by all of the ATTLIST definitions.
<!ENTITY % common.att
'id ID #IMPLIED
role NMTOKEN #IMPLIED'>
<!ELEMENT Person (FirstName, LastName)>
<!ATTLIST Person %common.att;>
<!ELEMENT FirstName (#PCDATA)>
<!ATTLIST FirstName %common.att;>
<!ELEMENT LastName (#PCDATA)>
<!ATTLIST LastName %common.att;>
Common Attributes in XML Schemas
This example shows how to implement the same document structure as the above examples using the
XSL Schema language Attributes Groups
<attribute name="id" type="ID"/>
<attribute name="role" type="NMTOKEN"/>
<complexType name="PersonType" content="elementOnly">
<element name="FirstName" type="xsf:NameType"/>
<element name="LastName" type="xsf:NameType"/>
<element name="Person" type="xsf:PersonType"/>
Global attributes often add many choices at any point in the document. Users have more selections available to them, and this could complicate authoring, however, some of the complexity that adding attributes to every element brings is lessened if these attributes are applied consistently. If users can expect the same common attribute on every element, it does not take a lot of extra effort to process the information. It will be easier to author or process a document that has a consistently applied set of common attributes on all elements compared to one that has inconsistently applied attributes on most of the elements.
An ID attribute is a typical use for a common attribute. It is useful to be able to reference any element in the document using this.
The common attributes are usually declared in a
to help maintainability.
XMLspec DTD Common Attributes Chapter
explains the use of common attributes in the DTD.
XHTML defines a set of core attributes. These attributes are applied to almost every element in the XHTML document type. The core attributes are defined like this:
<!ENTITY % coreattrs
"id ID #IMPLIED
class CDATA #IMPLIED
style %StyleSheet; #IMPLIED
title %Text; #IMPLIED"
Even the simple line break element has these attributes added to it:
<!ELEMENT br EMPTY> <!-- forced line break -->
Structuring XML Documents
, Section 3.2.4: Global Attributes.
Thanks to Hans C. Arents for supplying the correct XSL Schema definition used in the pattern.