Friday, 7 November 2014

xml : Derivation by Restriction


You can create new data types by adding restrictions on it. You can apply restriction on an element using <xs:restriction> element.

For Example
You can make sure that your employee age is greater than 18 and less than 56 by using restriction.

       <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>

The element on which restriction is applied is called base data type. In the above case base data type is integer.

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" />

        <!-- 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 name -->
  <xs:element name="name">
    <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: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="name" />
                            <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>
        <name>
            <firstname>Hari</firstname>
            <middlename>Krishna</middlename>
            <lastname>Gurram</lastname>
            <age>56</age>
            <salary>80000</salary>
            <married>single</married>
        </name>

        <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>

Employee.xml has age value as 56. So you can get below validation error.

XML validation started.
Checking file:/C:employee.xml...
Referenced entity at "file:/C:employees.xsd".
cvc-maxExclusive-valid: Value '56' is not facet-valid with respect to maxExclusive '56' for type '#AnonType_age'. [12]
cvc-type.3.1.3: The value '56' of element 'age' is not valid. [12]
XML validation finished.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment