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

Multiple restrictions

  1. XML Schema
  2. Relax NG
XML Schema keys: union, minInclusive, maxInclusive
Relax NG keys: choice, param

1. XML Schema

Now, we want the element "root" to be from the range 0-100 or 300-400 (including the border values). We will make a union from two intervals.

Valid document
Value is from interval 0-100.


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

Valid document
Value is from interval 300-400.


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

Invalid document
The value does not fall in the interval 0-100 neither 300-400.


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:simpleType>
      <xsd:union>
        <xsd:simpleType>
          <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="100"/>
          </xsd:restriction>
        </xsd:simpleType>
        <xsd:simpleType>
          <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="300"/>
            <xsd:maxInclusive value="400"/>
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:union>
    </xsd:simpleType>
  </xsd:element>
</xsd:schema>

2. Relax NG

As far as I know, such complex derivations are not possible in Relax NG. However, we can overcome this using the "choice" element. And this should be the same - logical OR corresponds to operation "union".

Valid document
Value is from interval 0-100.


<root xmlns="">50</root>

Valid document
Value is from interval 300-400.


<root xmlns="">380</root>

Invalid document
The value does not fall in the interval 0-100 neither 300-400.


<root xmlns="">500</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>
      <choice>
        <data type="integer">
          <param name="minInclusive">0</param>
          <param name="maxInclusive">100</param>
        </data>
        <data type="integer">
          <param name="minInclusive">300</param>
          <param name="maxInclusive">400</param>
        </data>
      </choice>
    </element>
  </start>
</grammar>