XMLPatterns.com

Role Attribute

Abstract

Sometimes the designer of a DTD can not foresee all of the needs that the author of a document will have. In order to give the author flexibility an attribute that specifies a role can be included on some of the elements.

Problem

Specialization of the document type sometimes needs to be made by an author. It is impossible for the designer of the DTD to foresee all of the uses that the authors have in mind. The designer may not even know where in the document the specialization will need to take place.

Context

For any document type that is intended for use outside a very limited context, users need flexibility to include uses which the designed of the document type has not foreseen.

Forces

By definition, document types constrain the choices that a document author can make. This constraint can at times be too limiting. Document users need flexibility to use the document type in ways not foreseen by the designer of the document type.

Solution

An attribute is created which can further describe the element. This role attribute further specializes the use of the Element.

Examples

        

<!ELEMENT Document (Paragraph)*>
<!ELEMENT Paragraph (#PCDATA)>
<!ATTLIST Paragraph Role CDATA>

        
      

Using the above DTD the author can create a document like this:

        

<Document>
<Paragraph Role="warning">
Do not attempt this at home.
</Paragraph>
<Paragraph>
The following is a set of
instructions for creating
a bungee jumping cord from
elastic bands ...
</Paragraph>
</Document>

        
      

The author intends for the "Do not attempt this at home" paragraph to appear emphasized in some way, for example in bold or in a different color. The DTD designer would have had no way of knowing the special needs of the author of the document, so a Role Attribute was placed on the paragraph, to allow for different types of paragraphs to exist.

Discussion

Including role attributes allows the author to have a great deal of flexibility. The flexibility may make the creation of processing software very difficult. Processing software will not be able to know in advance the intention of the author. However, by allowing customization of processing software through the use of scripts or stylesheet, this can be a very powerful pattern. Much of the power of XSLT (http://www.w3.org/TR/xslt) and CSS (http://www.w3.org/Style/) comes from being able to add Role Attributes.

Role attributes can be used to extend the document type without changing the structure of documents. This can help extend the life of the document type between revisions, and can be used to prototype new features of the document type.

Related Patterns

Role attributes are almost always used on Generic Element types. Generic Elements are created when the designer of a document type does not know all of the uses of that element, and the Role Attribute allows the author of a document to make the element more specialized to his or her use of the element.

Known Uses

XHTML uses several role attributes including class and meta element in XHTML.

The XMLspec DTD role attribute (http://www.w3.org/XML/1998/06/xmlspec-report-v21.htm#AEN620) shows a typical usage of this pattern.