XMLPatterns.com

Declare Before First Use

Abstract

Elements which are referenced by another part of a document should be found earlier in the document than the first place they are referenced.

Problem

Often an element will refer to another one; this is what the ID and IDREF attributes in XML are used for. A common example is a footnote element. When processing software encounters an reference to an element that has not yet been processed, it often has to do more processing.

Context

This pattern applies to a wide variety of document types. It is common to have elements reference one another.

Forces

Ease of processing is effected by element order. Having elements in the correct order can make processing easier.

Solution

Ensure that the elements are ordered so that elements are encountered before they are referred to. This gives the processing software a better chance of doing a single pass traversal of the document.

Examples

        

<!DOCTYPE Doc [

<!ELEMENT Person (Adress*, BillToAddress, ShipToAddress)>

<!ELEMENT Address (#PCDATA)>
<!ATTLIST Address id IDREF #REQUIRED >

<!ELEMENT BillToAddress EMPTY>
<!ATTLIST BillToAddress address IDREF #REQUIRED>

<!ELEMENT ShipToAddress EMPTY>
<!ATTLIST ShipToAddress address IDREF #REQUIRED>

]>

<Doc>
<Person>
<Address id="1">
123 Main St, New York NY
</Address>
<Address id="2">
2001 5th Ave, New York NY
</Address>
<BillToAddress address="1"/>
<ShipToAddress address="2"/>
</Person>
</Doc>

        
      

Discussion

Stream based processors are popular, particularly when documents are very large, and may take up large amounts of memory, or when speed of processing is essential. It is often difficult to use stream based processing if elements that are needed to process a section of the document do not appear until after the place they are needed. This requires a forward-looking mechanism that goes against the principle of stream based processing.

Related Patterns

Consider this pattern when applying the Referenced Note. The Metadata First is very similar to this patterns, except it deals with metadata instead of general elements as this pattern does.

Known Uses