In: Computer Science
Create a schema file that captures the requirements for a <student> element>
Here are the specifications for student:
3.1. A student must have a first name and last name.
3.2. A student may have a middle name, but it’s optional.
3.3. A student may have a home address, a work address, or both.
3.3.1. Use a complex type to connect to a single schema definition for “address”
3.4. An address has: a street address, city, state, and zip code. Include the following limits:
3.4.1. Street address may have one or two entries
3.4.2. State must be two letters
3.4.3. Zipcode must be 5 or 9
digits
Online searches can help with regular expressions for this one!
Sample files:
<?xml version="1.0" encoding="utf-8"?> <!--- Valid data --> <student> <first_name>Mani</first_name> <middle_name>Rupee</middle_name> <last_name>Patel</last_name> <home_address> <street>2525 Longhorn Way</street> <street>Apartment 35</street> <city>Redmond</city> <state>WA</state> <zip>88899-3535</zip> </home_address> </student> |
<?xml version="1.0" encoding="utf-8"?> <!--- Valid data --> <student> <first_name>Michael</first_name> <last_name>Hortensia</last_name> <home_address> <street>Generic Technical Company</street> <street>1515 Longhorn Way</street> <city>Seattle</city> <state>WA</state> <zip>98585</zip> </home_address> <work_address> <street>346 3rd Street</street> <city>Olympia</city> <state>WA</state> <zip>98581</zip> </work_address> </student> |
<?xml version="1.0" encoding="utf-8"?> <student> <!--- Invalid data --> <first_name>Mani</first_name> <work_address> <street>3535 Too many streets</street> <street>Rockford Way</street> <street>MacIntyre Lane</street> <city>Redmond</city> <state>WART</state> <zip>88899-35358</zip> </work_address> </student> |
Below is the student schema with all the mentioned contraint applied-
===================== XML SCHEMA START===================================
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<!-- Below 'required'
constraint is used to make element first_name and last_name
mandatory field -->
<xs:element name="first_name" type="xs:string"
use="required"/>
<xs:element name="middle_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"
use="required"/>
<xs:element name="home_address">
<xs:complexType>
<xs:sequence>
<!-- Below 'maxOccurs'
constraint is used to make element street to occur minimum 1 or
maximum 2 times -->
<xs:element name="street" type="xs:string"
maxOccurs="2"/>
<xs:element name="city" type="xs:string"/>
<!-- To apply constraint
on state for having only two letters , pattern is defined on
element state below -->
<xs:element name="state" type="xs:string">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z]{2,2}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<!-- To apply constraint
on zipcode for having integers of length 5 or 9 digits
only , pattern
is defined on element zip below -->
<xs:element name="zip">
<xs:simpleType>
<xs:restriction base="xsd:integer">
<xs:pattern value="(\d{5}(\d{9})?)"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- All same constraints like home_address element also defined on element work_address below -->
<xs:element name="work_address">
<xs:complexType>
<xs:sequence>
<xs:element name="street" type="xs:string"
maxOccurs="2"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z]{2,2}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="zip">
<xs:simpleType>
<xs:restriction base="xsd:integer">
<xs:pattern value="(\d{5}(\d{9})?)"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
===================== XML SCHEMA END ===================================
Please comment if any query.