Validating XML Elements Using xs:pattern Regex
The xs:pattern facet in XML Schema Definition (XSD)
provides a mechanism to enforce precise lexical constraints on XML
string elements using regular expressions. By restricting the base
xs:string or other simple types, schema authors can
validate that incoming XML element values conform strictly to defined
formats such as postal codes, phone numbers, or custom identifier codes.
This article explains how the xs:pattern facet works, its
regular expression syntax rules, and how XML parsers evaluate these
constraints.
The Role of
xs:pattern in XML Schema
In XSD, simple types represent element and attribute values. To
restrict the allowed text inside these elements, XML Schema provides
constraining facets. The xs:pattern facet defines a regular
expression that the entire lexical value of an XML element must match to
be considered valid.
The facet is declared inside an xs:restriction element
within an xs:simpleType definition:
<xs:simpleType name="ProductCodeType">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z]{3}-[0-9]{4}"/>
</xs:restriction>
</xs:simpleType>In this example, an XML element using ProductCodeType is
valid only if its string value consists of exactly three uppercase
letters, a hyphen, and four digits (e.g., ABC-1234).
Implicit Whole-String Anchoring
A defining characteristic of regular expressions in XSD is that they
are implicitly anchored to the beginning and end of the string. In
standard regex engines, patterns match substrings unless explicitly
bounded with anchors like ^ and $. In XSD:
[A-Z]{3}automatically behaves as^[A-Z]{3}$.- The regular expression must match the complete string content from start to finish.
- Using explicit anchors like
^or$is unnecessary and generally treated as literal characters or syntax errors in standard XSD validation.
Supported Regular Expression Syntax
The regular expression dialect used by xs:pattern is
defined by the W3C XML Schema specification and includes standard regex
constructs:
- Character Classes: Standard single characters,
ranges (e.g.,
[a-zA-Z]), and character class subtraction (e.g.,[a-z-[aeiou]]for consonants). - Predefined Classes: Escape sequences such as
\d(digits),\D(non-digits),\s(whitespace),\S(non-whitespace),\w(word characters), and\W(non-word characters). - Quantifiers: Standard repetition operators such as
?(0 or 1),*(0 or more),+(1 or more), and exact counts{n},{n,}, or{n,m}. - Alternation and Grouping: Parentheses
()for grouping and the pipe symbol|for logical OR operations. - Unicode Categories: Support for Unicode property
classes using
\p{Category}and\P{Category}(e.g.,\p{Lu}for uppercase letters across multiple languages).
Evaluation with
Multiple xs:pattern Facets
When multiple xs:pattern elements are defined within the
same xs:restriction, the XML validator evaluates them using
a logical OR relationship. The element is valid if its
value matches at least one of the specified patterns.
<xs:simpleType name="ZipCodeType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{5}"/>
<xs:pattern value="\d{5}-\d{4}"/>
</xs:restriction>
</xs:simpleType>In this definition, a value matching either a 5-digit format
(12345) or a 9-digit format (12345-6789) will
pass validation.
Validation Process During XML Parsing
During schema validation, the XML parser reads the target element’s
text content after character normalization is applied according to the
xs:whiteSpace facet rules of the underlying base type. The
parser passes the normalized string directly to the XSD regular
expression engine. If the pattern matches the entirety of the normalized
text, the element is valid; if it fails, the parser generates a schema
validation error and rejects the document.