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;>
<?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>
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.
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; >