XMLPatterns.com

Multi Root Document Types

Abstract

A single document type with multiple root elements is used to handle varying documents that may exist within a system.

Problem

A system needs to handle varying types of documents. These are somehow related, yet each type of document needs a unique identity provided by a document root.

Context

Systems where varying types of documents need to be processed, but the different documents are closely related.

Forces

Maintainability of the system is effected by how the document types are organized.

Solution

Create a single declaration with multiple root elements for the different document types.

Examples

The two documents below show two different bank transactions, a deposit and a withdrawal. The document have different root elements, but many other common elements. Both of these documents use the same DTD, but declare different roots.
        

<!DOCTYPE "Deposit" SYSTEM "bank.dtd">
<Deposit>
<Account id="123"/>
<Amount currency="USD">100.00</Amount>
<Date>05-10-2000</Date>
</Deposit>

        
      
        

<!DOCTYPE "Withdrawal" SYSTEM "bank.dtd">
<Withdrawal>
<Account id="123"/>
<Amount currency="USD">100.00</Amount>
<Date>05-10-2000</Date>
</Withdrawal>

        
      

Discussion

Allows the different document types to easily share structure. Having all of the related document types declared in a single file can increase maintainability. However, if revisions need to be made to the different document types separately, it might be better having separate declarations.

Related Patterns

Universal Root and Multiple Document Types are alternatives.

Known Uses

In DocBook (http://www.oasis-open.org/docbook/) the root element can be a set or a book element.