Restriction types in XML Schema

This article describe about several type of restrictions in XML Schema
  • 2663

Other Restrictions on a Series of Values

 In XML schema also different technique to take restriction. These are given below which accept the value is ZERO or MORE occurrence of  LOWERCASE letter from a to z.

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

 
Note: Above declared  element "Message" take restriction on schema.

Example 2

<xs:element name="Message">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="([a-z][A-Z])+"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

 
Note: Second example also defines an element called "Message" with a restriction. This element accept  value is one or more pairs of letter, each pair consist of a lower case letter followed by an upper case letter. For example, "sTART"  must  be validated by this pattern, but not "Start" or "START" or "start":

Example 3

Note: Third example show that an element  "Sex" with some restriction. It is only accept value male Or female:

<xs:element name="Sex">

  <xs:simpleType>

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

      <xs:pattern value="male|female"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 
Example 4

 <xs:element name="password">

  <xs:simpleType>

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

      <xs:pattern value="[a-zA-Z0-9]{7}"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 
Note: This example show that an element "password" declared with some restriction. It take "7" character long password  weather in lowercase or uppercase from "a" to "z" or number from 0 to 8.

Restrictions on Whitespace Characters

In this topic define how whitespace  characters should be handled, For this we have to use the whitespace constraint.

 <xs:element name="location">

  <xs:simpleType>

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

      <xs:whiteSpace value="preserve"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 
Note: The above declared example called "location" with some restriction. The whitespace constraint is set to "preserve" this means XML  processor will not remove any whitespace.

 Example 1

<xs:element name="location">

  <xs:simpleType>

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

      <xs:whiteSpace value="replace"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

Note: This example also define an element called "location" with a restriction. The whitespace constraint is set to "replace" this means that XML processor will "REPLACE" if any whitespace occur either in character  or line or paragraph.

Example 2

<xs:element name="location">

  <xs:simpleType>

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

      <xs:whiteSpace value="collapse"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 
Note: This  is also another example define an element called "location" with a  restriction. The whitespace constraint is set to "collapse" this means that XML processor will "REMOVE" all whitespace characters (tabs, multi-line space, single-line space etc.

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.