By using
whitespace constraint you can specify how whitespace characters should be
handled. <xs:whitespace> supports below values preserve, replace,
collapse.
Value
|
Description
|
preserve
|
XML
processor will not remove any white spaces.
|
replace
|
XML
processor replaces line feeds, tabs, spaces, and carriage returns with
spaces.
|
Collapse
|
XML
processor removes all white space characters like line feeds, tabs, spaces,
carriage returns. Trailing and leading spaces are removed. Multiple spaces
are replaced by single space.
|
<!-- validate description --> <xs:element name="description"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse" /> </xs:restriction> </xs:simpleType> </xs:element>
Above
snippet, collapse all the white spaces in the element description.
employee.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 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="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>
No comments:
Post a Comment