Restriction element is used to define accepted values that an XML element can take.
Syntax
<xs:restriction base = "element-type"> restrictions </xs:restriction>
base | Type of the Element on which restriction is to be applied. For example,<xs:restriction base = “xs:integer”> specifies that this restriction is specific to an element of type int. |
restriction | restriction is normally a range of conditions to be applied on the element’s value. In this example, we’ve set a restriction on marks that marks should be in range of 0 to 100 with both values are included.<xs:minInclusive value = “0”/> <xs:maxInclusive value = “100”/> |
Examples
Restriction on Value.
Condition − Marks should be in range of 0 to 100.
<xs:element name = "marks"> <xs:simpleType> <xs:restriction base = "xs:integer"> <xs:minInclusive value = "0"/> <xs:maxInclusive value = "100"/> </xs:restriction> </xs:simpleType> </xs:element>
Restriction on Set of Values.
Condition − Grades should only be A, B or C.
<xs:element name = "grades"> <xs:simpleType> <xs:restriction base = "xs:string"> <xs:enumeration value = "A"/> <xs:enumeration value = "B"/> <xs:enumeration value = "C"/> </xs:restriction> </xs:simpleType> </xs:element>
Restriction using regular pattern.
Condition − firstname should be in alphabets only.
<xs:element name = "firstname"> <xs:simpleType> <xs:restriction base = "xs:string"> <xs:pattern value = "[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element>
Types of Restrictions
S.No. | Restriction & Description |
---|---|
1 | enumerationDefines a list of values which are acceptable. |
2 | fractionDigitsDefines the maximum number of decimal places allowed(zero or more). |
3 | lengthDefines length in terms of characters of string or items in a list(zero or more). |
4 | maxExclusiveDefines upper bounds for numeric values excluding this number. |
5 | maxInclusiveDefines upper bounds for numeric values including this number. |
6 | maxLengthDefines maximum length in terms of characters of string or items in a list(zero or more). |
7 | minExclusiveDefines lower bounds for numeric values excluding this number. |
8 | minInclusiveDefines lower bounds for numeric values including this number. |
9 | minLengthDefines minimum length in terms of characters of string or items in a list(zero or more). |
10 | patternDefines the exact sequence of characters identified by the pattern that are acceptable |
11 | totalDigitsDefines the exact number of digits allowed in the number(always greater than zero) |
12 | whiteSpaceDefines the way in which white space characters (line feeds, tabs, spaces, and carriage returns) are handled |
Pingback: XSD - Simple Types - Adglob Infosystem Pvt Ltd