Coding/Learn/xml/xsd data types and restrictions
XML & Schemas Foundational Course
01.Introduction & Tree Structure
02.Syntax & Well-Formedness Rules
03.Elements vs. Attributes & Best Practices
04.Namespaces (xmlns) & Prefix Resolution
05.CDATA Sections & XML Comments
06.DTD (Document Type Definition) & Entities
07.Schema Definition (XSD) & Simple Elements
08.XSD Complex Types & Order Indicators (sequence, choice, all)
09.XSD Restrictions, Facets & Pattern Matching (Regex)
10.DOM Architecture & Node Types
11.Accessing & Traversing XML DOM Nodes
12.Modifying, Creating & Deleting XML Nodes
13.DOMParser, XMLSerializer & Parsing Errors
14.AJAX & XMLHttpRequest (responseXML)
15.XPath Syntax, Predicates & Axes
16.XSLT Transformations & Templates (<xsl:template>)
17.XSLT Logic, Loops & Sorting (for-each, if, choose)
18.XQuery & FLWOR Expressions (For, Let, Where, Order, Return)
19.in Web Services: SOAP, WSDL, RSS & SVG
20.Capstone: Enterprise XML Data Pipeline & Validator
Course Certificate0%

Complete all lessons to earn

LearnXSD Restrictions, Facets & Pattern Matching (Regex)
9 / 20 0%
Lesson 09 of 20
6 mins read

XSD Restrictions, Facets & Pattern Matching (Regex)

PreviousNext Lesson
In Plain English

XSD restrictions (facets) define acceptable boundaries for element and attribute values. You can restrict numeric ranges (minInclusive, maxInclusive), define allowed string enumerations (enumeration), constrain lengths, and enforce regular expression patterns (pattern).

The Mental Model

"Restrictions are the velvet rope bouncers of your data schema: they verify that an age is between 18 and 120, a password matches a regex pattern, or a currency code is strictly USD, EUR, or GBP."

Deep Dive: How It Works

<xs:restriction base="xs:string">: Base type being constrained.

<xs:enumeration value="..."/>: Constrains value to an explicit set of allowable choices.

<xs:pattern value="[0-9]{3}-[A-Z]{2}"/>: Validates against a regular expression pattern.

Numeric Facets: <xs:minInclusive value="0"/> and <xs:maxInclusive value="100"/>.

Syntax Blueprint

<xs:simpleType name="AgeType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="18" />
  </xs:restriction>
</xs:simpleType>

Creates a custom simple type requiring integer values >= 18.

PointCore Rules to Remember

Point star
Value Constraining: Enforces business rules directly at the schema validation layer.
Point star
Enumerations: Limits values to specific allowed enum constants (e.g. pending, approved, rejected).
Point star
Regex Patterns: Enforces exact formats for postal codes, phone numbers, and SKUs.

Common Beginner Traps & How to Fix Them

Mistake:Applying string length facets to integer types.

Why it happens: <xs:length> applies to strings; numeric types must use <xs:totalDigits> or <xs:maxInclusive>.

How to fix: Use the correct facet for the base data type.

Pro Tip: Use <xs:whiteSpace value="collapse"/> to automatically strip leading, trailing, and duplicate whitespace before validation.

Live Interactive Example

Hit Run Code to see it live
Restricted Password and Status Schema
XML 1.0 / XSD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: Enforces strict enum values and formatted SKU codes matching [A-Z]{3}-[0-9]{4}.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Define an integer score restriction

  • PointCreate a <xs:restriction base="xs:integer"> with <xs:minInclusive value="0"/> and <xs:maxInclusive value="100"/>.
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates