XMLPatterns.com

Container Element

Abstract

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.

Problem

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

Context

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

Forces

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

Solution

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

Examples

        

<ComputerConfiguration>
<RAM>128 MB</RAM>
<WordProcessor>WordPerfect</WordProcessor>
<HardDriveSize>8GB</HardDriveSize>
<XMLParser>Xerces</XMLParser>
</ComputerConfiguration>

        
      
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:
        

<ComputerConfiguration>
<Software>
<Wordprocessor>Wordperfect</Wordprocessor>
<XMLParser>Xerces</XMLParser>
</Software>
<Hardware>
<RAM>128 MB<RAM>
<HardDriveSize>8 GB</HardDriveSize>
</Hardware>
</ComputerConfiguration>

        
      
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.

Discussion

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.

References

See Structuring XML Documents, Section 5.2.1: Containers.