Printable Version


If the same information is included at many different points in a document the information can be placed in just one place, and shared from each place in the document that needs to refer to it.


Placing the same information in many different place can cause several problems:
  • Mistakes can be made in copying the information
  • If the data changes, all occurrences of the information must be located and changed. This makes maintenance of the document difficult.
  • Documents can become quite large if the same information is repeated over and over again.


This is a very general pattern, almost any document type can make use of it. This pattern can be used anywhere where the same information must be repeated more than once in a document. The repeated data can occur in the document declaration, or in the document instance itself.


This pattern can effect the length of the document, maintainability, and readability of the document.


Place the shared information in just one place, and make references to it from every place in the document that needs to include this information. There are several ways to do this:
  • Use XML Entity Declarations. These provide a way to do text substitutions inside of a document. The XML Specification Section 4.2 Entity Declarations
  • Use the XLink attribute xlink:show="embed". This technique has the advantage of using a standard, so some tools may be available to do the processing. This is defined in The W3C's XML Linking Language Specification
  • Use ID and IDREF attributes. A reference can be made to an entity via an IDREF attribute. Applications processing this will need to do the processing to do this type of Flyweight.
See the Examples section for an example of each of the above techniques.


XML Entities
This example shows how XML Entities can be used to put a piece of information in just one place, and then have it appear in multiple places in the document.

<!ENTITY TITLE "My Document">
This is my paragraph.


This document shows the use of XLink attributes to include the contents of a document in two different places. Since XLink is a standard it is possible that existing tools could be used to do the substitution.

<include xlink:show="embed"
<include xlink:show="embed"
This is my paragraph.


This shows how ID and IDREF attributes can be used as a Flyweight. Note thats this would require some extra work on the part of the processing software to do the substitution.

<text id="title">My Document</text>
<title IDREF="title"/>
<h1 IDREF="title"/>



This pattern can greatly enhance the maintainability of a document, if the same data is repeated several times, and that data changes, every occurrence of that data must be found and updated. This can be a tedious and error prone task. Having the data in only one place allows all occurrences of it to be changed at once.

The Flyweight pattern should not be used if the shared information can vary over time. This would increase the maintenance burden of the document.

Readability of the document can suffer if the Flyweight pattern is used, readers are forced to reference a different section of the document when looking at the contents.

Related Patterns:

See Declare Before First Use for suggestions on where to place the shared information.

Known Uses:

XHTML uses a a common attributes parameter entity that is a Flyweight.

The XML & SGML Cookbook page 1-126 mentions the Flyweight pattern.

Visit our sponsors: (Interested in advertising on XMLPatterns.com?)