<!-- validate mobileNum --> <xs:element name="empNum"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:totalDigits value="9" /> </xs:restriction> </xs:simpleType> </xs:element>
Above
snippet validates “empNum” element such that, the total number of digits
shouldn’t exceed 9.
employees.xsd
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- Schema for simple elements --> <xs:element name="id" type="xs:integer" /> <xs:element name="firstname" type="xs:string" /> <xs:element name="middlename" type="xs:string" /> <xs:element name="lastname" type="xs:string" /> <xs:element name="salary" type="xs:decimal" /> <xs:element name="married" type="xs:string" /> <xs:element name="department" type="xs:string" /> <xs:element name="pjtname" type="xs:string" /> <xs:element name="from" type="xs:date" /> <xs:element name="to" type="xs:date" /> <xs:element name="city" type="xs:string" /> <xs:element name="district" type="xs:string" /> <xs:element name="state" type="xs:string" /> <xs:element name="country" type="xs:string" /> <!-- validate mobileNum --> <xs:element name="empNum"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:totalDigits value="9" /> </xs:restriction> </xs:simpleType> </xs:element> <!-- validate descendents --> <xs:element name="descendants"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="1" /> <xs:maxInclusive value="7" /> </xs:restriction> </xs:simpleType> </xs:element> <!-- validate taxEmption --> <xs:element name="taxExemption"> <xs:simpleType> <xs:restriction base="xs:float"> <xs:maxExclusive value="250000.1" /> </xs:restriction> </xs:simpleType> </xs:element> <!-- validate description --> <xs:element name="description"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse" /> </xs:restriction> </xs:simpleType> </xs:element> <!-- validate pin --> <xs:element name="pin"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5" /> <xs:maxLength value="8" /> </xs:restriction> </xs:simpleType> </xs:element> <!-- validate sex --> <xs:element name="sex"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="male" /> <xs:enumeration value="female" /> </xs:restriction> </xs:simpleType> </xs:element> <!-- Applying restriction on Employee age --> <xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="19" /> <xs:maxExclusive value="56" /> </xs:restriction> </xs:simpleType> </xs:element> <!-- Schema for element details --> <xs:element name="details"> <xs:complexType> <xs:sequence> <xs:element ref="firstname" /> <xs:element ref="middlename" /> <xs:element ref="lastname" /> <xs:element ref="age" /> <xs:element ref="salary" /> <xs:element ref="married" /> <xs:element ref="sex" /> <xs:element ref="address" /> </xs:sequence> </xs:complexType> </xs:element> <!-- schema for address --> <xs:element name="address"> <xs:complexType> <xs:sequence> <xs:element ref="city" /> <xs:element ref="district" /> <xs:element ref="state" /> <xs:element ref="country" /> <xs:element ref="pin" /> </xs:sequence> </xs:complexType> </xs:element> <!-- Schema for element project --> <xs:element name="project"> <xs:complexType> <xs:sequence> <xs:element ref="department" /> <xs:element ref="pjtname" /> <xs:element ref="from" /> <xs:element ref="to" /> </xs:sequence> </xs:complexType> </xs:element> <!-- Schema for element employee --> <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element ref="id" /> <xs:element ref="description" /> <xs:element ref="taxExemption" /> <xs:element ref="descendants" /> <xs:element ref="empNum" /> <xs:element ref="details" /> <xs:element ref="project" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> <!-- Schema for element employees --> <xs:element name="employees"> <xs:complexType> <xs:sequence> <xs:element ref="employee" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
employee.xml
<?xml version="1.0" encoding="UTF-8"?> <employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="employees.xsd"> <employee> <id>1</id> <description> I am IT professional with 3Yrs of Experience </description> <taxExemption>240000</taxExemption> <descendants >3</descendants > <empNum>123456789</empNum> <details> <firstname>Hari</firstname> <middlename>Krishna</middlename> <lastname>Gurram</lastname> <age>25</age> <salary>80000</salary> <married>single</married> <sex>male</sex> <address> <city>Ongole</city> <district>prakasam</district> <state>Andhra Pradesh</state> <country>India</country> <pin>523169</pin> </address> </details> <project> <department>aero</department> <pjtname>Flight Controls</pjtname> <from>2011-09-16</from> <to>2013-01-22</to> </project> <project> <department>banking</department> <pjtname>Online Transaction Processing System</pjtname> <from>2013-01-23</from> <to>2014-10-06</to> </project> <project> <department>mobile</department> <pjtname>Daily News Application</pjtname> <from>2014-10-07</from> <to>2015-05-06</to> </project> </employee> </employees>
No comments:
Post a Comment