15.2 Simple Templates
The pattern facet element holds a pattern in its value attribute. The simplest possible form of pattern involves a series of characters that must be present, in the order specified, in each element or attribute declaration that uses the data type constrained by the pattern facet.
The pattern 'abc' might be specified as the fixed value of a Code element:
<Code>abc</Code>
The pattern '0-201-41999-8' might be specified as the fixed value of an ISBN attribute:
<Book ISBN="0-201-41999-8" ... >
In this simple form, a pattern is similar to an enumeration, except that in the case of patterns the match must be exact, regardless of the data type used (recall that Section 14.6 explains how patterns differ from enumerations in this respect).
Although specifying an exact sequence of characters is among the simplest things that can be achieved with the pattern language, specifying a sequence of characters that must not appear in a value is much harder.
It is often a good idea to use the 'normalized' or 'token' data type as the base data type for the restriction when the presence of surrounding whitespace should not be allowed to trigger an error.
Just as a restriction element can contain multiple enumeration elements, it can also contain multiple pattern elements. The element content or attribute value is valid if it matches any of the patterns:
<restriction base="token"> <pattern value="abc" /> <pattern value="xyz" /> </restriction> <Code>abc</Code> <!-- OK --> <Code>xyz</Code> <!-- OK --> <Code> abc </Code> <!-- OK --> <Code>acb</Code> <!-- ERROR --> <Code>xzy</Code> <!-- ERROR --> <Code>abcc</Code> <!-- ERROR -->
Alternatively, a single pattern can contain multiple 'branches'. Each branch is actually a distinct, alternative expression, separated by the '|' symbol from previous or following branches. Again, the pattern test succeeds if any one of the branches matches the pattern (the '|' symbol is therefore performing a function similar to its use in DTD content models). The following example is equivalent to the multipattern example above:
<restriction base="string"> <pattern value="abc|xyz" /> </restriction>
Note that, although branches are never essential at this level, because multiple pattern elements can be used instead, they are the only technique available in another circumstance discussed later (involving subexpressions).