XMLPatterns.com

Universal Root

Abstract

Provide a single root element that contains an option of multiple elements. Often used for different transaction types within a single document type.

Problem

There are several different types of document that need to be processed. Providing a single root element allows for all of the different types of document to be defined by a single DTD. When a processor receives this document, it will know which type of document to expect by inspecting the child element of the root element.

Context

Universal Root is useful when a system has several different, but related, document types to be processed. A transactional processor would be an example of such a system.

Forces

Having multiple, distinct document types can increase the maintenance burden of a system. Document types that may be related, but are declared in separate files can start to stray from commonalities that can be taken advantage of.

Solution

Create a document type that has a single root element.

Examples

        

<!ELEMENT Transaction
(AddAddress | RemoveAddress | UpdateAddress)>

<!ELEMENT AddAddress
(AddressBookEntry)>

<!ELEMENT RemoveAddress
(AddressID)>

<!ELEMENT UpdateAddress
(AddressID, AddressBookEntry)>


<Transaction>
<AddAddress>
<AddressBookEntry> ... </AddressBookEntry>
</AddAddress>
</Transaction>

        
      
In this example an online address book application is being developed. There are several transactions that can take place, lick adding new entries, deleting entries, updating entries.

Discussion

This allows a way for several related types of documents to be grouped into a single document type structure. A processing system will always know which DTD to expect, but can still handle the documents differently based on the first nested element type. This makes sharing common definitions between the different document easy.

This is also a way that multiple XML document can be gathered into a single larger document. For example a log file might consist of several fragments, for example:

        

<Log>
<Time>Jan 15, 1999 10:58AM</Time>
<Event>Startup</Event>
</Log>
<Log>
<Time>Jan 15, 1999 11:02AM</Time>
<Event>Message Received from Bob</Event>
</Log>
...

        
      
Since there is no root element in the file it cannot be processed "as is". To process this, one can prepend a start tag, for example "<LogFile>", in front of the file, and append the end tag "</LogFile>" to the end of the file to be able to process it as a single XML document.

Related Patterns

Multi Root Document Types and Multiple Document Types are alternatives to this pattern.

Known Uses