Wednesday, February 18, 2026

RESTRICTION in XSD

 RESTRICTION in XSD:

XSD Restrictions/Facets


1) minInclusive
2) maxInclusive
3) minExclusive
4) maxExclusive
5) totalDigits
6) fractionDigits
7) pattern
8) whiteSpace
9) enumeration
10) length
11) maxLength
12) minLength

In XML Schema Definition (XSD), you can define various constraints and restrictions on XML elements and attributes using different facets. Facets are XML Schema components that further constrain the data types of elements or attributes. Here are some commonly used facets that allow you to impose restrictions in XSD:

Length: The length facet specifies the exact number of characters or list items allowed.

<xs:element name="example">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:length value="5" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

MinLength and MaxLength: These facets define the minimum and maximum number of characters or list items allowed.

<xs:element name="example">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:minLength value="3" />
      <xs:maxLength value="10" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Pattern: The pattern facet specifies a regular expression that the value must match.

<xs:element name="example">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Za-z0-9]+" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Enumeration: The enumeration facet restricts the value to a list of acceptable values.

<xs:element name="example">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Value1" />
      <xs:enumeration value="Value2" />
      <xs:enumeration value="Value3" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

MinExclusive, MinInclusive, MaxExclusive, and MaxInclusive: These facets define the minimum and maximum allowable values, where Exclusive means the value cannot be equal to the specified limit, and Inclusive means the value can be equal to the limit.

<xs:element name="example">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1" />
      <xs:maxExclusive value="100" />
    </xs:restriction>
  </xs:simpleType>

 Mandatory attribute:

Syntax:

<attribute name="id" type="int" use="required"/>   

// if 'use' is not their then it means this field is optional by default.

Use the #REQUIRED keyword if you don't have an option for a default value

occurrence constraints of elements in XML documents

 minOccurs, maxOccurs, and unbounded are attributes that are used to define the occurrence constraints of elements in XML documents. They specify how many times an element must or can appear within its parent element. These attributes are applied to <xs:element> declarations in an XSD schema.

minOccurs: This attribute defines the minimum number of occurrences of an element. If minOccurs is set to 0, the element is optional and may not appear in the XML document. If minOccurs is set to a positive integer (e.g., 1), the element is required and must appear at least that number of times.

<xs:element name="example" minOccurs="0"/>
In this example, the element <example> is optional.

maxOccurs: This attribute defines the maximum number of occurrences of an element. If maxOccurs is set to a positive integer (e.g., 5), the element can appear at most that number of times. If it is set to "unbounded", the element can appear any number of times (including zero).

<xs:element name="example" maxOccurs="5"/>
In this example, the element <example> can appear at most 5 times.

unbounded: The string "unbounded" is a special value for the maxOccurs attribute, indicating that there is no upper limit to the number of occurrences of the element. When maxOccurs is set to "unbounded", the element can appear any number of times.

<xs:element name="example" maxOccurs="unbounded"/>
In this example, the element <example> can appear any number of times, including zero (optional), one or more.

The default values for minOccurs and maxOccurs are 1. 

maxoccurs="unbounded" //means that particular element can appear n number of type.

maxOccurs="2" // means element appears 2 times

Ex:

<element name="age" type="string" maxOccurs="unbounded"></element>

<element name="name" type="string" maxOccurs="2"></element>

minOccurs="0" // Its optional element.

minOccurs="0" maxOccures="2"  // It should appear either 0times or 2 times.

Note-if minOccurs & maxOccurs are not defined it means it should appear at-list once.


1) xsd:element minOccurs="1" name="asdf"/> 

Note1: if you specify only minOccurs attribute, it can't be greater than 1, because the default value for maxOccurs is 1. 

2)  <xsd:element maxOccurs="2" name="asdf"/>

Note2: if you specify only maxOccurs attribute, it can't be smaller than 1, because the default value for minOccurs is 1.

3)  <xsd:element minOccurs="5" maxOccurs="2" name="asdf"/>

- its invalid

4)   <xsd:element minOccurs="0" maxOccurs="0"/>

It is a valid combination which makes the element prohibited.

Common Case:

1)            <xsd:element name="A"/>

Means A is required and must appear exactly once.

2)            <xsd:element name="A" minOccurs="0"/>

Means A is optional and may appear at most once.

3)            <xsd:element name="A" maxOccurs="unbounded"/>

Means A is required and may repeat an unlimited number of times.

4)            <xsd:element name="A" minOccurs="0" maxOccurs="unbounded"/>

Means A is optional and may repeat an unlimited number of times.

To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement.


Ristriction Syntax:

<element name="element name">

<simpleType>

<restriction base="datatype">

use the above constraints

</restriction>

</simpleType>

</element>

Ex 1: 10000>=, 90000<=

  <element name="salary">

  <simpleType>

   <restriction base="decimal">

            <minInclusive value="10000">

            <maxInclusive value="90000">

            </restriction>

   </simpleType>

 </element>

Ex 2: acceptable value is 3 characters only (A-Z (or) a-z)

<element name="lastName">

 <simpleType>

  <restriction base="string">

    <pattern value="[A-Za-z][A-Za-z][A-Za-z]"/>

  </restriction>

 </simpleType>

</element>

Ex 3: acceptable value is 5 digit : <56100>

<element name="Zipcode">

 <simpleType>

  <restriction base="string">

    <pattern value="[0-9][0-9][0-9][0-9][0-9]"/>  //or value="[0-9]{5}"

  </restriction>

 </simpleType>

</element>

Ex 4: acceptable value is M or F

<element name="gender">

 <simpleType>

  <restriction base="string">

    <pattern value="[MF]"/>

  </restriction>

 </simpleType>

</element>

Ex 5: acceptable value is MALE or FEMALE

<element name="gender">

 <simpleType>

  <restriction base="string">

    <pattern value="[MALE|FEMALE]"/>

  </restriction>

 </simpleType>

</element>

Ex 6:

<xs:simpleType name="string1000">

      <xs:restriction base="xs:string">

         <xs:maxLength value="1000"/>

      </xs:restriction>

   </xs:simpleType>   

Person Name: it must be at least 2 character and not more than 30 character.

 Must contains alphabetic character,-,’periods,and spaces

 

<xs:element name=”name”>
                <xs:simpleType>
                                <xs:restriction base=”xs:string”>
                                                <xs:minLength value=”2”/>
                                                 <xs:maxLength value=”30”/>
                                                <xs:pattern value=”[A-Za-z\-.’]{2,30}”/>
                                </xs: restriction>
                </xs:simpleType>
</xs:element>

PhoneNumber:               

Common hyphenated field separation and dots or spaces for field separator. 

<xs:element name=”usPhone”>
                <xs:simpleType>
                                <xs:restriction base=”xs:string”>
<xs:pattern value=”\(?\d{3}\)?[ \-\.]?\d{3}[  \-\.]?\d{4}”/>
                                                <xs:minLength value=”10”/>
                                                 <xs:maxLength value=”16”/>                                   
                                </xs: restriction>
                </xs:simpleType>
</xs:element>
 

EmailAddress: 

<xs:element name=”email”>
                <xs:simpleType>
                                <xs:restriction base=”xs:string”>
                                           <xs:pattern value=”(\w+\.)*\w+@(\w+\.)+[A-Za-z]{2,9}” />
                                             <xs:minLength value=”6”/>
                                             <xs:maxLength value=”255”/>                                 
                                </xs: restriction>
                </xs:simpleType>
</xs:element>

 POSTAL-CODE:

Ex:85266-1234 

<xs:element name=”usZip”>
                <xs:simpleType>
                                <xs:restriction base=”xs:string”>
                                                <xs:pattern value=”\d{5}(-\d{4})?”  />
                                                <xs:minLength value=”5”/>
                                                 <xs:maxLength value=”10”/>                                   
                                </xs: restriction>
                </xs:simpleType>
</xs:element>

Regular expression:

\d    Any digit 0-9

\D    This disjoint of \d

\s     space or tab character

\S     the disjoint of \s

\w    a word:upper- or lowercase letter,number, or underscore

\W     disjoint of \ws

.        Any single characters

?       zero or one occurrence

*zero or more occurrences

+ one or more occurrences 

{}  indicates  the number of times  to match the preceding pattern

                Ex: \d{3} matches any 3 digit in row

[]  matches any pattern inside the brackets

                Ex:

[AB]  matches single A or B character (case sensitive) and nothing else.

[1-5]  matches single value of  1 to 5

[1-5A-E]   matches single value of  1 to 5 or A,B,C,D,E

[^0]  matches anything but 0

No comments:

Post a Comment

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...