Next / Previous / Contents / TCC Help System / NM Tech homepage

3. Named patterns: factoring the schema

Nesting element patterns inside other element patterns, as the example above shows, is not the best way to write a complex schema. This style is hard to understand for the same reason monolithic programs are hard to understand: too much complexity in one place.

Fortunately, RNC provides an easy, natural way to factor the schema into small, easily understood parts. When you write a schema, you can define named patterns, and assemble them like building blocks into a complete schema.

This RNC construct defines a named pattern:

name = pattern

where the name follows XML naming rules (letters, digits, underscore, dollar sign, hyphen, and period).

Here's a rewrite of the example pattern above using named patterns. We start by defining a named pattern named start that describes the pattern of the whole document:

## Describes one park; the root element.
start = park

This defines the special pattern named start as having the same structure as another pattern named park, which we define next:

park = element park
{ attribute name { text },
  trail*
}

The four lines above define the named pattern park as containing an element named park, which has a required text attribute called name, and contains zero or more trail patterns.

Note that park is used in two different ways in the lines above. It is the name of a pattern we're defining, but it is also the name of an element. We don't have to use the same name in both places. However, in many cases, if a named pattern is the same as an element, using the same name for both is clear.

RNC schemas use two different namespaces: pattern names exist only inside the schema file, but element names correspond to the names of elements in an XML document.

Next we define the trail pattern:

## Each trail element describes one trail in the park.
trail = element trail
{ attribute climb { "easy" | "medium" | "hard" },
  attribute distance { text },
  text
}

The lines above define the content of the named trail pattern as a single trail element, which has two attributes named climb and distance. The content of the trail element can be any text.