Skip to main content
XMLDir

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

Schemas and validity

Facet

A constraint that narrows a simple type: a length, a range, a regular expression, a list of permitted values.

Facets are how a schema says something specific about a value rather than merely naming its kind. xs:string becomes a product code with a pattern facet; xs:integer becomes a rating with minInclusive and maxInclusive.

Which facets apply depends on the base type — you cannot put a length facet on a number — and they compose, so a type may carry several. When one fails, the validator names the facet, which is the most useful diagnostic in the whole language.

Defined in
XSD 1.0 §4.3
Category
Schemas and validity
Also written
constraining facet, xs:pattern, xs:enumeration

Shown, not asserted

All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.

  • Facets are what make a type specific: unfaceted xs:string accepts anything.

    schema.xsd
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="v" type="xs:string"/>
    </xs:schema>
    value
    ZZ-99999

    What the engine returns

    Valid as xs:string.
  • The value's kind is still checked first — a facet cannot rescue the wrong type.

    schema.xsd
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="v" type="xs:integer"/>
    </xs:schema>
    value
    1.5

    What the engine returns

    Invalid as xs:integer.

What catches people out

  • XSD regular expressions are anchored: the pattern must match the whole value, and there is no need for ^ or $.
  • Facets apply to the whitespace-processed value, so a leading space may already be gone by the time a pattern is checked.
  • An enumeration on a numeric type compares values, not strings: 1.0 and 1 are the same enumerated 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.