XML technologies / XML Schema / Regular expressions for specifying simple datatypes / Quantification

(Excerpt from "XML Schema" by Eric van der Vlist)

Despite their superficial similarity, facet xs:pattern interprets its value attribute very differently from xs:enumeration. xs:enumeration reads the value as a lexical representation and converts it to a value of the base datatype. xs:pattern instead reads the value as conditions that lexical values must satisfy. Writing

<xs:pattern value="15"/>

specifies three conditions: the first character is “1”, the second “5”, then the string ends. Each condition, such as the first character being “1”, is called a piece. This is only the simplest way to specify a piece.

Each pattern piece consists of an atom identifying a character or a set of characters, plus an optional quantifier. Characters are the simplest atoms, except special characters, which must be escaped. Our example omitted quantifiers. They can be specified with a special character (* for zero or more, + for one or more, ? for zero or one), or a range in braces ({n} means exactly n occurrences, {n,m} means n to m occurrences, inclusive, or {n,} means n or more occurrences).

These quantifiers let us express our three patterns as one:

<xs:simpleType name="myByte">
  <xs:restriction base="xs:byte">
    <xs:pattern value="1?5?"/>
  </xs:restriction>
</xs:simpleType>

This new pattern requires zero or one “1” followed by zero or one “5”. It differs from our earlier three patterns because it accepts the empty string. However, since the empty string is not in the lexical space of our base type ( xs:byte ), the new datatype has the same lexical space as the old one.

Quantifiers can also limit leading zeros. This pattern permits at most two:

<xs:simpleType name="myByte">
  <xs:restriction base="xs:byte">
    <xs:pattern value="0{0,2}1?5?"/>
  </xs:restriction>
</xs:simpleType>

   

 

 

 

back to top