ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 1 / 8 << | Prev | Next |
Contents > Definition of own types > Definition of a custom simpleType - temperature must be greater than -273.15

Definition of a custom simpleType - temperature must be greater than -273.15

  1. XML Schema
  2. Relax NG
XML Schema keys: restriction, base, double, minExclusive
Relax NG keys: data, type, param

1. XML Schema

The element "T" must contain number greater than -273.15. We will define our custom type for temperature named "Temperature" and will require the element "T" to be of that type.

Valid document


<T xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > -10 </T>

Invalid document
The number is less than -273.15.


<T xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > -300.1 </T>

Correct XML Schema (correct_0.xsd)


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

  <xsd:element name="T" type="Temperature"/>

  <xsd:simpleType name="Temperature">
    <xsd:restriction base="xsd:double">
      <xsd:minExclusive value="-273.15"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

2. Relax NG

The element "T" must contain number greater than -273.15. We will define a custom pattern named "Temperature" and use it for the definition of the element "T".

Valid document


<T xmlns=""> -10 </T>

Invalid document
The number is less than -273.15.


<T xmlns=""> -300.1 </T>

Correct Relax NG schema (correctRelax_0.rng)


<grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" xmlns="http://relaxng.org/ns/structure/1.0" >

  <start>
    <element>
      <name ns="">T</name>
      <ref name="Temperature"/>
    </element>
  </start>

  <define name="Temperature">
    <data type="double">
      <param name="minExclusive">-273.15</param>
    </data>
  </define>
</grammar>