Despite an apparent similarity, the xs:pattern facet interprets its value attribute in a very different way than xs:enumeration does. xs:enumeration reads the value as a lexical representation, and converts it to the corresponding value for its base datatype, while xs:pattern reads the value as a set of conditions to apply on lexical values. When we write:
<xs:pattern value="15"/>
we specify three conditions (first character equals "1," second character equals "5," and the string must finish after this). Each of the matching conditions (such as first character equals "1" and second character equals "5") is called a piece. This is just the simplest form to specify piece.
Each piece in a pattern is composed of an atom identifying a character, or a set of characters, and an optional quantifier. Characters (except special characters that must be escaped) are the simplest form of atoms. In our example, we have omitted the quantifiers. Quantifiers may be defined using two different syntaxes: either a special character (* for 0 or more, + for one or more, and ? for 0 or 1) or a numeric range within curly braces ({n} for exactly n times, {n,m} for between n and m times, or {n,} for n or more times).
Using these quantifiers, we can merge our three patterns into one:
<xs:simpleType name="myByte"> <xs:restriction base="xs:byte"> <xs:pattern value="1?5?"/> </xs:restriction> </xs:simpleType>
This new pattern means there must be zero or one character ("1") followed by zero or one character ("5"). This is not exactly the same meaning as our three previous patterns since the empty string "" is now accepted by the pattern. However, since the empty string doesn't belong to the lexical space of our base type (xs:byte), the new datatype has the same lexical space as the previous one.
We could also use quantifiers to limit the number of leading zeros--for instance, the following pattern limits the number of leading zeros to up to 2:
<xs:simpleType name="myByte"> <xs:restriction base="xs:byte"> <xs:pattern value="0{0,2}1?5?"/> </xs:restriction> </xs:simpleType>
Copyright © 2002 O'Reilly & Associates. All rights reserved.