Container Element


A container has multiple elements as child elements. A new element type is created to group related elements. This is a very general pattern and many other patterns specialize this one.


There are multiple elements that occur at the same level within a document. These elements can be separated into distinct groups.


There are multiple elements that can occur as child elements of a single, higher level element, and some of these elements are related.


Documents need structure, and this is the easiest way to add structure to a document.


Create a new element that contains some of the multiple child elements.



<RAM>128 MB</RAM>

The above document describes a computer's configuration. If however processing software was only interested in the hardware aspects of the configuration it would need to check all of the elements and extract the RAM and HardDriveSize elements. Compare this to the following:

<RAM>128 MB<RAM>
<HardDriveSize>8 GB</HardDriveSize>

In this example the software and hardware aspects of the configuration are separated. Processing software that need to extract individual aspects can do so easily.


This pattern adds a level of abstraction to the structure of a document. This abstraction can be used to simply provide grouping of elements as in the Examples section. This grouping provides additional semantic information about the data.
This extra element can also be a useful place to provide metadata about the individual group of data.

Related Patterns

The Marketplace pattern can be used if there is more than one category that can apply to each type.
The Head-Body pattern uses two specific types of Container Elements, one for metadata and one for content.
The Collection Element is a container for single type of element.

Known Uses

The Isogen whitepaper Object-Oriented SGML Deconstructing SGML for Storage Section (http://www.isogen.com/papers/oosgml.html#storage) contains an an excellent example of where a container was used to help in the storage and retrieval of data.


See Structuring XML Documents, Section 5.2.1: Containers.