XSD facet
pattern
A regular expression the whole value must match — anchored implicitly, and in XSD's own dialect rather than PCRE.
A pattern facet constrains the lexical space with a regular expression. It is anchored at both ends implicitly: there is no ^ or $, and adding them matches literal characters rather than positions.
The dialect is XSD's own. It has no lookahead, no backreferences and no lazy quantifiers, but it does have character class subtraction and Unicode block and category escapes that PCRE lacks.
Multiple pattern facets on one type are combined with OR, which is the opposite of how every other facet composes.
- Lexical space
- An XSD regular expression.
- Derives from
- Primitive type
- Category
- Constraining facets
What catches people out
- It is implicitly anchored. Writing ^\d+$ matches a literal caret and dollar sign.
- No lookahead, no backreferences, no lazy quantifiers.
- Two pattern facets on the same type mean 'either', not 'both'. Every other facet intersects.
- The pattern applies to the lexical form after whitespace processing, so on an xs:token the value is already collapsed.
The schema used
This is the exact schema the literals above are validated against. Paste it into the validator with one of them to see the result yourself.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="t"><xs:restriction base="xs:string"><xs:pattern value="[A-Z]-\d{4}"/></xs:restriction></xs:simpleType>
<xs:element name="v" type="t"/>
</xs:schema>Related
- enumerationA closed list of permitted values, compared in the value space — so 01 and 1 are the same number.
- xs:tokenA string trimmed of leading and trailing whitespace, with internal runs collapsed to one space. The right type for most identifiers.
- xs:stringAny sequence of legal XML characters, with whitespace preserved exactly. The only string type that does not touch your value.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.