ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 2 / 10 << | Prev | Next |
Contents > Simple types > Restrictions

Restrictions

  1. XML Schema
  2. Relax NG
XML Schema keys: restriction

1. XML Schema

Restricting simpleType is relatively easy. Here we will require the value of the element "root" to be integer and less than 25.

Valid document


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

Invalid document
The value must be less than 25.


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:simpleType>
      <xsd:restriction base="xsd:integer">
        <xsd:maxExclusive value="25"/>
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:element>
</xsd:schema>

2. Relax NG

Relax NG can use the XML Schema datatypes. They can be imported using the "datatypeLibrary" attribute. Restrictions are performed using the "param" element. Let's have the same example as above - "root" must be an integer and less than 25.

Valid document


<root xmlns="">24</root>

Invalid document


<root xmlns="">25</root>

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="">root</name>
      <data type="integer">
        <param name="maxExclusive">25</param>
      </data>
    </element>
  </start>
</grammar>

Correct Relax NG schema (correctRelax_1.rng)
This short version is also valid.


<element ns="" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" name="root" xmlns="http://relaxng.org/ns/structure/1.0" >
  <data type="integer">
    <param name="maxExclusive">25</param>
  </data>
</element>