Metadata First
Abstract
Metadata should appear in a document before the data which it is about.
Problem
Many processors (for example SAX based parsers) are stream based. That is, the document is processed as it is read in, the whole document is never held in memory at the same time. If a piece of metadata appears after the data it refers to, the application software will need to store the data, then read the metadata in order to know what to do with the data.
Context
Anywhere in the document that metadata appears.
Forces
The ease and speed of processing a document can be effected by the order in which the structures of the document appear.
Solution
Always make sure that metadata appears in a document before the data it refers to. This can be done whether the metadata is attribute data or element data.
For example, with the following code,
<Heading>
This is a new section<Font size="3"/>
</Heading>
If the processing software were trying to send this information to a printer, it would need to save the text of the heading, read in the font size, send the font size command to the printer, and then send the text to the printer. If the font element appeared before the text, there would be no need to save the text.
Examples
This example shows a table element with the height and width elements appearing before the actual data of the table. This allows the processing software to start rendering the table as the data is read in. If the height and width of the table came after the table data, the processing software would need to read in and save all the data before starting to display it.
<Table>
<height unit="pixels">100</height>
<width unit="pixels">100</width>
<Row><Col>2</Col><Col>3</Col></Row>
<Row><Col>8</Col><Col>7</Col></Row>
</Table>
Discussion
Placing the metadata before the data that it is about will often simplify processing software. This is also often a natural way for people to think about the data, making documents easier to read and author.
Related Patterns
The ordering of the head and body element in the
Head-Body pattern is an application of this pattern.
Known Uses
In XHTML The
head, which contains metadata, and
body which contains the content are always in that order. The
table elements contain size and spacing data in the attributes of the
table element, which places it before the data, and the caption and column width and spacing data appears before the table data itself.