Use of Restrictions in XML Schema

This article describe about restrictions in XML Schema.
  • 2587

Schema Restrictions/Facets

Restriction are used to define some condition which must be follow when assign the value of element or attributes. This restriction on elements are called faces.

Example:

This example show that restriction on the salary element. The value of salary element must not be the lower of  "2000" and grater then "20,000"

 

 <xs:element name="salary">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2000"/>
      <xs:maxInclusive value="20,000"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>


Restrictions on a Set of Values

When we will have to take restriction on the set of XML element value. Then use enumeration constraint.

The example below given an element called "bike" with a restriction. The only acceptable values are: Honda, Yamaha, bajaj:

<xs:element name="bike">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Honda"/>
      <xs:enumeration value="Yamaha"/>
      <xs:enumeration value="bajaj"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

The example above could also have been written like this:

Note: The above written example we can also write in this formate

<xs:element name="bike" type="bikeType">
  <xs:simpleType type="bikeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Honda"/>
      <xs:enumeration value="Yamaha"/>
      <xs:enumeration value="bajaj"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>


Restrictions on a Series of Value

To take limitation on the content of XML element to define series of number or letter which can be used, for this used the patter constraint.

The given example below define an element  "LETTER" with a restriction. it is only accept value is ONE of the UPPERCASE letter from A to Z.

<xs:element name="LETTER">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

 Example given below that accept three value in LOWERCASE

<xs:element name="BEGIN">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-z][a-z][a-z]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

 Example given below that accept three value in UPPERCASE

<xs:element name="BEGIN">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z][A-Z][A-Z]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

 Example given below that accept three value weather in LOWERCASE or UPPERCASE

<xs:element name="BEGIN">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Below given  next example defines an element called " Yourchoice" with a restriction. This is only acceptable value is ONE of the given letters: p, q, OR r

<xs:element name="yourchoice">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[pqr]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

The given below next example define an element called "predefine" with a restriction. It can accept only FOUR digit value in a series , and every digit range must be in 0 to 7

<xs:element name="predefine">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:pattern value="[0-7][0-7][0-7][0-7]"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.