XML technologies / XML Schema / Regexes for specifying simple datatypes / Common patterns

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

Having surveyed pattern notation, let us examine common patterns you may need to use or adapt in schemas, or simply study as examples.

String datatypes

Regular expressions handle information in its textual form, making them excellent for restricting strings.

Unicode blocks

Unicode is a major XML advantage, but few applications correctly process and display every Unicode character, and fewer users can read them all. To check that your string datatypes belong to particular Unicode blocks, derive them from base types such as:

<xs:simpleType name="BasicLatinToken">
  <xs:restriction base="xs:token">
    <xs:pattern value="\p{IsBasicLatin}*"/>
  </xs:restriction>
</xs:simpleType>
<xs:simpleType name="Latin-1Token">
  <xs:restriction base="xs:token">
    <xs:pattern value="[\p{IsBasicLatin}\p{IsLatin-1Supplement}]*"/>
  </xs:restriction>
</xs:simpleType>

These patterns do not impose an encoding on the document. For example, Latin-1Token can validate instance documents encoded in UTF-8, UTF-16, ISO-8869-1, or other encodings, assuming the characters belong to the blocks BasicLatin and Latin-1Supplement. These patterns constrain the lexical space after parser transformations, not the instance document’s physical format.

Counting words

We saw how a dummy enumeration derivation counts words, but it counts only whitespace-separated words, treating punctuation like ordinary characters. Patterns can restrict word count. Define an atom as one or more word characters (\w+) followed by one or more nonword characters (\W+), then constrain how often it occurs. For permissive punctuation, allow leading nonword characters and a final word without a separator. Avoid end ambiguity by handling the final word separately and making trailing separators optional:

<xs:simpleType name="story100-200words">
  <xs:restriction base="xs:token">
    <xs:pattern value="\W*(\w+\W+){99,199}\w+\W*"/>
  </xs:restriction>
</xs:simpleType>

URIs

As discussed, Einschränkungen does not resolve relative URIs, so requiring easier-to-process absolute URIs may help. Applications may also restrict allowed URI schemes, easily done with:

<xs:simpleType name="httpURI">
  <xs:restriction base="xs:anyURI">
    <xs:pattern value="http://.*"/>
  </xs:restriction>
</xs:simpleType>

Numeric and floating-point types

Numeric types are not strictly text, but patterns can constrain their lexical representations.

Leading zeros

Removing leading zeros is straightforward, but preserving an optional sign and zero itself requires care. Use a pattern such as:

<xs:simpleType name="noLeadingZeros">
  <xs:restriction base="xs:integer">
    <xs:pattern value="[+-]?([1-9][0-9]*|0)"/>
  </xs:restriction>
</xs:simpleType>

This redefines every lexical rule for integers. Applied to xs:token, it produces the same lexical space as with xs:integer. Alternatively, rely on the base datatype:

<xs:simpleType name="noLeadingZeros">
  <xs:restriction base="xs:integer">
    <xs:pattern value="[+-]?([^0].*|0)"/>
  </xs:restriction>
</xs:simpleType>

Relying on the base type simplifies patterns but can complicate interpretation, requiring readers to combine its lexical rules with the pattern’s rules.

Fixed format

The maximum digit count can be set with xs:totalDigits and xs:fractionDigits. These facets specify maxima and work on the value space. To constrain lexical format, for example DDDD.DD, use:

<xs:simpleType name="fixedDigits">
  <xs:restriction base="xs:decimal">
    <xs:pattern value="[+-]?.{4}\..{2}"/>
  </xs:restriction>
</xs:simpleType>

Dates and times

Date/time lexical representations are complex. Patterns give developers additional control.

Time zones

Time-zone support in W3C XML Schema is controversial and needs restrictions to avoid comparison problems. Patterns remain simple because the schema validator already checks date/time syntax. To require time zones, check that the time portion ends in Z or contains a sign:

<xs:simpleType name="dateTimeWithTimezone">
  <xs:restriction base="xs:dateTime">
    <xs:pattern value=".+T.+(Z|[+-].+)"/>
  </xs:restriction>
</xs:simpleType>

To forbid time zones, simply check that the time portion contains none of +, -, or Z:

<xs:simpleType name="dateTimeWithoutTimezone">
  <xs:restriction base="xs:dateTime">
    <xs:pattern value=".+T[^Z+-]+"/>
  </xs:restriction>
</xs:simpleType>

Both types use T as separator, after which signs can occur only in the time zone. Dates without times lack T, but their time zones can be recognized by the colon instead:

<xs:simpleType name="dateWithTimezone">
  <xs:restriction base="xs:date">
    <xs:pattern value=".+[:Z].*"/>
  </xs:restriction>
</xs:simpleType>
<xs:simpleType name="dateWithoutTimezone">
  <xs:restriction base="xs:date">
    <xs:pattern value="[^:Z]*"/>
  </xs:restriction>
</xs:simpleType>

Applications can also enumerate allowed time zones:

<xs:simpleType name="dateTimeInMyTimezones">
  <xs:restriction base="xs:dateTime">
    <xs:pattern value=".+\+02:00"/>
    <xs:pattern value=".+\+01:00"/>
    <xs:pattern value=".+\+00:00"/>
    <xs:pattern value=".+Z"/>
    <xs:pattern value=".+-04:00"/>
  </xs:restriction>
</xs:simpleType>

We promised to revisit xs:duration and define two totally ordered types: durations in years and months, and durations in days, hours, minutes, and seconds. Test for D or T: without either, only years and months occur. The other type cannot simply forbid Y and M, since M also marks minutes. Instead, check that the first field after an optional sign is days or T:

<xs:simpleType name="YMduration">
  <xs:restriction base="xs:duration">
    <xs:pattern value="[^TD]+"/>
  </xs:restriction>
</xs:simpleType>
<xs:simpleType name="DHMSduration">
  <xs:restriction base="xs:duration">
    <xs:pattern value="-?P((\d+D)|T).*"/>
  </xs:restriction>
</xs:simpleType>

   

 

 

 

back to top