Name

xs:pattern — Allows you to specify value constraints using a regular expression

Applies to

All datatypes

Attributes

value

An xs:string containing a regular expression defined using the rules set out in http://www.w3.org/TR/xmlschema-2/#regexs

Operation

During validation, the XML Schema processor compares the value of the element or attribute against the regular expression specified in the value attribute of the xs:pattern element. If the regular expression matches the content, the content is valid; otherwise, it’s not. (If multiple xs:pattern facets are specified, the content is valid if it matches any of them.) The incredible flexibility of regular expressions makes it possible to create a wide variety of types that include mixed text and numbers, as well as types that must have particular patterns of punctuation.

XML Schema regular expressions operate in much the same way as regular expressions in Perl 5.6 and later, except that they implicitly anchor the expressions at the head and tail. To avoid this behavior, include the characters .* at the beginning or end of the expression. Table 1-2Table 1-4 list the most commonly used aspects of XML Schema regular expressions as well as some Unicode-specific material that’s much more frequently used in XML Schema processing than in other uses of regular expressions.

Table 1-2. Commonly used regular expression constructs

Pattern

Meaning

(String)

A value that matches String

String1 | String2

A value that matches String1 or String2

String?

Zero or one ...

Get XML Pocket Reference, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.