ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 7 / 8 << | Prev | Next |
Contents > Definition of own types > Element represents time duration

Element represents time duration

  1. XML Schema
  2. Relax NG
XML Schema keys: duration, minInclusive, maxInclusive

1. XML Schema

The element "A" must represents time duration, which is from one day to one week. We will define our custom type named "myDuration" and will require the element "A" to be of that type.

Valid document
Two days, three hours and twenty minutes.


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

Valid document
Twenty six hours is OK too (day has 24 hours).


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

Invalid document
Six hours is not enough.


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

Invalid document
Eight days is too much.


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

Correct XML Schema (correct_0.xsd)


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

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

  <xsd:simpleType name="myDuration">
    <xsd:restriction base="xsd:duration">
      <xsd:minInclusive value="P1D"/>
      <xsd:maxInclusive value="P7D"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

2. Relax NG

Valid document
Two days, three hours and twenty minutes.


<A xmlns="">P2DT3H20M</A>

Valid document
Twenty six hours is OK too (day has 24 hours).


<A xmlns="">PT26H</A>

Invalid document
Six hours is not enough.


<A xmlns="">PT6H</A>

Invalid document
Eight days is too much.


<A xmlns="">P8D</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="myDuration"/>
    </element>
  </start>

  <define name="myDuration">
    <data type="duration">
      <param name="minInclusive">P1D</param>
      <param name="maxInclusive">P7D</param>
    </data>
  </define>
</grammar>