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
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.
In this example, the element <example> is optional.
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: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: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: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: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