ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 6 / 8 << | Prev | Next |
Contents > Definition of own types > Element contains date

Element contains date

  1. XML Schema
  2. Relax NG
XML Schema keys: date, minInclusive

1. XML Schema

The element "A" must contain a date, which is not less than 1.1.2001. We will define our custom type for the string named "myDate" and will require the element "A" to be of that type.

Valid document


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

Invalid document
The date is out of bounds.


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

Invalid document
This is not a valid date.


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="A" type="myDate"/>

  <xsd:simpleType name="myDate">
    <xsd:restriction base="xsd:date">
      <xsd:minInclusive value="2001-01-01"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

2. Relax NG

We will use the "date" datatype from XML Schemas and restrict it using using "minInclusive". The element "A" must contain this pattern.

Valid document


<A xmlns="">2001-01-01</A>

Invalid document
The date is out of bounds.


<A xmlns="">1998-10-12</A>

Invalid document
This is not a valid date.


<A xmlns="">02-10-12</A>

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="">A</name>
      <ref name="myDate"/>
    </element>
  </start>

  <define name="myDate">
    <data type="date">
      <param name="minInclusive">2001-01-01</param>
    </data>
  </define>
</grammar>