XMLPatterns.com

Common Attributes

Abstract

Provide a set of attributes that can be placed on all, or most, elements in the document type.

Problem

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.

Context

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.

Forces

The usability of the document type depends a lot on consistency, and this pattern can be used to add consistency.

Solution

Create a common set of attributes that will be used by all or most of the elements in the document type.

Examples

Common Attributes in DTDs

This examples shows a DTD that has two common attributes, id and 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 (http://www.w3.org/TR/xmlschema-0/#AttrGroups) .
          

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/1999/XMLSchema"
targetNamespace="http://www.xmlpatterns.com/XMLSchema-fragment"
xmlns:xsf="http://www.xmlpatterns.com/XMLSchema-fragment">

<attributeGroup name="CommonAtts">
<attribute name="id" type="ID"/>
<attribute name="role" type="NMTOKEN"/>
</attributeGroup>

<complexType name="NameType">
<attributeGroup ref="CommonAtts"/>
</complexType>

<complexType name="PersonType" content="elementOnly">
<element name="FirstName" type="xsf:NameType"/>
<element name="LastName" type="xsf:NameType"/>
<attributeGroup ref="CommonAtts"/>
</complexType>

<element name="Person" type="xsf:PersonType"/>

</schema>

          
        

Discussion

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.

Related Patterns

The common attributes are usually declared in a Flyweight to help maintainability.

Known Uses

The XMLspec DTD Common Attributes Chapter (http://www.w3.org/XML/1998/06/xmlspec-report-v21.htm#cmnatts) 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 -->
<!ATTLIST br
%coreattrs;
>

          
        

References

See 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.