Skip to main content
XMLDir

Search 261 pages — tools, formats, elements, namespaces, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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 validates

Every literal below is put through the schema validator on each test run, against the schema at the foot of this page.

  • A-1001

What does not

  • 1001

    Does not match the pattern in the example schema.

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.

schema.xsd
<?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>

Open the XSD validator

Related

All 29 datatypes and facets

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.