How to include Compositor, Carnality and Facets in XML Schema?
We have learnt to write XML document and create Schema and DTD for the same. Now its time to extend the schema with different types of Compositor, Cardinality and Facets in XML Schema.
Before that lets understand what those terms means:
Compositor is the same thing what we define for the type of XML element. It can be either simpleType or complexType. 'simpleType ' is used if the element has single child element and 'complexType' is used if the XML element has many child elements or attributes.
See the code below:
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="roll" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Here, the element has two child elements, roll and name that's why we use compositor of complex type.
Now so what is carnality in XML Schema ?
Carnality refer to the number of times and element can appear in a XML DDT or Schema. Suppose the the number of student in element 'students' can range from 1 to 35 only then this is cardinality. We can use "minOccurs" and "maxOccurs" to include the cardinality. To understand lets see the snippet below:
<!--Asiignment 4 : Schema for the XML-->
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:restriction base="student">
<xs:minOccur value="1"/> <!-- This is cardinality: It must have at least one stuent-->
<xs:maxOccur value="35"/> <!-- This is cardinality: It can have at max 35 stuent-->
</xs:restriction>
<xs:element name="name" type="xs:string"/>
<xs:element name="roll" type="xs:int"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="gender" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
By default the value of minOccur is 0 and maxOccur is unbounded (no limit for maximum count.) In the code above it has been commented out what part is called cardinality.Now let's move toward our last topic facets. Facets are nothing but something that sets bounds, limits and restrictions in XML Schema.
The restriction is done in many basis such as
<xs:element name="name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Here we can see we have use the length facet to fix the length of name. It means it can have maximum of 30 characters for the value of name. Now lets see what does enumeration means: It simply means that the value can only be among the already defined enumerations. Suppose we have to accept the value of Gender, then can we accept something like "ABCD.." ? Obviously NO. So for such situation we fix the value to be selected among Male, Female and Other.
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<xs:enumeration value="Female"/>
<xs:enumeration value="Other"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
So after above declaration of facet value of Gender we cannot put the Value other than Male, Female and Other. Where can we use Patterns ? Patterns can be used whenever the data needs to accepted in a certain pattern. See the example below:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Here, the regex "([a-z])*" means that we can input lowercase letters from a to z and nothing else. For whitespaces there are three options:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
So using all these concepts in single schema file we will obtain the following schema file. We already have published an article on how to write schema and DTD which you can read from the link below:
BCA TU Chapter 3: How to write XML Schema and DTD?
For now we will use the previous schema file whic was made for students data to add compositors, cardinality and facets.
<!--Assignment 5: Extend your Assignment 4 to include compositor, cardinality and facets-->
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:simpleType> <!--Simple Type because students has only student element-->
<xs:sequence> <!-- This means all the child elements should be in sequence, We can also use 'all' OR 'choice'-->
<xs:element name="student">
<xs:restriction base="student">
<xs:minOccur value="1"/>
<xs:maxOccur value="35"/>
</xs:restriction>
<xs:complexType> <!-- Because student has many child elements or attributes-->
<xs:sequence> <!-- sequence (must be in seq), all (must be all elements, choice (min one element)-->
<xs:element name="name" type="xs:string">
<xs:restriction base="string">
<xs:minLength value="3"/>
<xs:maxLength value="30"/>
<xs:whiteSpace value="collapse"/>
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:element>
<xs:element name="roll" type="xs:integer">
<xs:restriction base="integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="35"/>
</xs:restriction>
</xs:element>
<xs:element name="address" type="xs:string">
<xs:restriction base="string">
<xs:minLength value="3"/>
<xs:maxLength value="30"/>
<xs:whiteSpace value="collapse"/>
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:element>
<xs:element name="gender" type="xs:string">
<xs:restriction base="string">
<xsd:enumeration value="Male"/>
<xsd:enumeration value="Female"/>
<xsd:enumeration value="Other"/>
</xs:restriction>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
After getting few good response from this site, we decided to extend our service and thus developed an app called BCA Guides Nepal. The app has been published on play store. This app provides features like
- Downloading Notes
- Downloading Syllabus
- Downloading Question papers
- Downloading Books
- Downloading Notices
- Checking Results within the app
App Download Link : https://play.google.com/store/apps/details?id=com.skthakur.bcaguidesnepal
0 Comments