ooRelaxNG: a simple object-oriented schema language

Fortune Cat

MURATA Makoto (eb2m-mrt@asahi-net.or.jp)

Copyright © 2002 MURATA Makoto

Distributed under GPL

1. Introduction

ooRelaxNG is an object-oriented schema language for objects represented in the XML syntax. Basic constructs of ooRelaxNG are objects, classes, instances, variables, and single inheritance. Validation against an ooRelaxNG schema is performed by first converting the schema to a RELAX NG schema and then performing RELAX NG validation. An XSLT styelsheet for converting ooRelaxNG schemas to RELAX NG schemas is publicly available.

1.1 What is ooRelaxNG

  1. ooRelaxNG is an object-oriented schema language for data. Data and schemas are both represented in the XML syntax.
  2. ooRelaxNG is a special-purpose schema languages. Just like RDF Schema handles RDF metadata rather than XML documents, ooRelaxNG handles objects rather than XML documents.
  3. First-class citizens of ooRelaxNG are objects, classes, variables. Elements and attributes are used as representations of such first-class citizens.
  4. Conversion from ooRelaxNG data to Java, C#, or C++ objects is not difficult, although it has not been implemented yet.
  5. Conversion from ooRelaxNG schemas to Java, C#, or C++ clases is straighforward, although it has not been implemented yet.

1.2 What ooRelaxNG is not

  1. ooRelaxNG is not an extension of RELAX NG. It is a different schema language, although many constructs of RELAX NG are borrowed. I do not intend to create an extension of RELAX NG by incorporating classes. I think that classes and non-terminals (names of patterns) belong to completely different meta models, and cannot easily co-exist in a schema language.
  2. ooRelaxNG is not a general-purpose schema language. It addresses XML-representations of objects, classes, and variables, but does not directly address XML. As a result, ooRelaxNG is totally inappropriate for handling XHTML, TEI, and DocBook, for example.

1.3 Why do I do ooRelaxNG?

I am a co-editor of the RELAX NG specification, which is a a simple and powerful general-purpose schema language for XML. Then, why am I doing another schema language?

I believe that special purpose schema language have many advantages. Many people would like to represent their data in XML and handle such data by Java, C#, or C++ programs. Hence, ooRelaxNG.

1.4 Copyright

Although I retain the copyrights for the XSLT stylesheet, I claim no rights in grammars generated by the XSLT stylesheet.

2. Example

Here is an ooRelaxNG schema. It has four schemas.
<?xml version="1.0"?>
<grammar
  xmlns:a="http://relaxng.org/ns/annotation/1.0"
  xmlns="http://www.xml.gr.jp/xmlns/ooRelaxNG-schema"
  ns="http://www.xml.gr.jp/xmlns/ex">

  <class name="animal" abstract="true"/>

  <class name="mammal" base="animal" abstract="true"/>

  <class name="cat" base="mammal">
    <optional>
      <attribute name="color"/>
    </optional>
  </class>

  <class name="fortuneCat" base="cat">
    <attribute name="raisedPaw">
      <a:documentation>
         right: inviting money
      </a:documentation>
      <a:documentation>
         left:  inviting people
      </a:documentation>
      <choice>
        <value>right</value>
        <value>left</value>
      </choice>
    </attribute>
  </class>

  <start>  
    <element name="schemaLanguage">
      <optional>
        <element name="mascot" class="animal"></element>

      </optional>
      <element name="specifications">
        <list>
          <zeroOrMore>
            <data type="string"/>
          </zeroOrMore>
        </list>
      </element>
    </element>
  </start>

</grammar>

An instance valid against this schema is shown below.

<schemaLanguage
    xmlns="http://www.xml.gr.jp/xmlns/ex" 
    xmlns:oi="http://www.xml.gr.jp/xmlns/ooRelaxNG-instance">
  <mascot oi:class="fortuneCat" color="white" raisedPaw="left"/>
  <specifications/>
</schemaLanguage>

In the above example, instance variables color and raisedPaw are represented by attributes. We can rewrite this schema so that they are represented by elements.

  <class name="cat" base="mammal">
    <optional>
      <element name="color"><text/></element>
    </optional>
  </class>

  <class name="fortuneCat" base="cat">
    <element name="raisedPaw">
      <a:documentation>
         right: inviting money
      </a:documentation>
      <a:documentation>
         left:  inviting people
      </a:documentation>
      <choice>
        <value>right</value>
        <value>left</value>
      </choice>
    </element>
  </class>

A valid instance is also changed as follows:

<schemaLanguage
    xmlns="http://www.xml.gr.jp/xmlns/ex" 
    xmlns:oi="http://www.xml.gr.jp/xmlns/ooRelaxNG-instance">
  <mascot oi:class="fortuneCat">
    <color>white</color>
    <raisedPaw>left</raisedPaw>
  <mascot/>
  <specifications/>
</schemaLanguage>

3. Brief tutorial

1) Classes

To use ooRelaxNG, use the namespace http://www.xml.gr.jp/xmlns/ooRelaxNG-schema.

A class is represented by an os:class elements, where os denotes "http://www.xml.gr.jp/xmlns/ooRelaxNG-schema".

os:class can have two attributes, abstract and base. A class can have instances unless abstract="true" is specified. The value of the attribute base references to a super class. Lack of this attribute means that the class does not have super classes.

Instance variables are declared by <element> or <attribute>. If an instance variable is declared by <element>, its value is represented as elements. If an instance variable is declared by <attribute>, its value is represented by attributes. An <element> or <attribute> can have an optional attribute class. The value is a class name. An instance of this class or its subclasses can be used as the value of an element or attribute matching the <element> or <attribute>.

2) Instances

If an element e matches <element> having the attribute class, this element represents an object of the specified class. One can assume that the tag name of e is a variable name and attributes and subelements of e collectively represent the object.

A reserved attribute oi:class denotes the class of this object, where oi denotes the namespace "http://www.xml.gr.jp/xmlns/ooRelaxNG-instance". Note that this object may be an instance of a subclass of the class specified in the schema.

4. Conversion to RELAX NG

A ooRelaxNG schema is converted to a RELAX NG grammar. Each class is represented by a define element. From each class, two names described by define are created. One represents the substructure of the class and the oher represents the class hierarchy.

The following RELAX NG schema is created by an XSLT Stylesheet from the first example and then editied manually.

<?xml version="1.0" encoding="utf-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0" ns="http://www.xml.gr.jp/xmlns/ex">
  
   <div xmlns:a="http://www.xml.gr.jp/xmlns/annotation">
      <a:documentation>Abstract superclass: animal</a:documentation>
      <define name="animal">
         <notAllowed/>
      </define>
      <define name="_animal">
         <empty/>
      </define>
   </div>

   <div
        xmlns:a="http://www.xml.gr.jp/xmlns/annotation">
      <a:documentation>Abstract superclass: mammal</a:documentation>
      <define name="animal" combine="choice">
         <ref name="mammal"/>
      </define>
      <define name="mammal">
         <notAllowed/>
      </define>
      <define name="_mammal">
         <interleave>
            <ref name="_animal"/>
         </interleave>
      </define>
   </div>

   <div
        xmlns:a="http://www.xml.gr.jp/xmlns/annotation">
      <a:documentation>Concrete class: cat</a:documentation>
      <define combine="choice" name="mammal">
         <ref name="cat"/>
      </define>
      <define name="cat">
         <attribute>
            <name ns="http://www.xml.gr.jp/xmlns/ooRelaxNG-instance">class</name>
            <value>cat</value>
         </attribute>
         <ref name="_cat"/>
      </define>
      <define name="_cat">
         <interleave>
            <ref name="_mammal"/>
            <optional>
               <attribute name="color"/>
            </optional>
         </interleave>
      </define>
   </div>

   <div
        xmlns:a="http://www.xml.gr.jp/xmlns/annotation">
      <a:documentation>Concrete class: fortuneCat</a:documentation>
      <define combine="choice" name="cat">
         <ref name="fortuneCat"/>
      </define>
      <define name="fortuneCat">
         <attribute>
            <name ns="http://www.xml.gr.jp/xmlns/ooRelaxNG-instance">class</name>
            <value>fortuneCat</value>
         </attribute>
         <ref name="_fortuneCat"/>
      </define>
      <define name="_fortuneCat">
         <interleave>
            <ref name="_cat"/>
    
            <attribute name="raisedPaw">
               <a:documentation xmlns:a="http://relaxng.org/ns/annotation/1.0">
                 right: inviting money
               </a:documentation>
      
               <a:documentation xmlns:a="http://relaxng.org/ns/annotation/1.0">
                 left:  inviting people
               </a:documentation>
               <choice>
                  <value>right</value>
                  <value>left</value>
               </choice>
            </attribute>
         </interleave>
      </define>
   </div>

   <start>  
      <element name="schemaLanguage">
         <optional>
            <element name="mascot">
               <ref name="animal"/>
            </element>
         </optional>
         <element name="specifications">
            <list>
               <zeroOrMore>
                  <data type="string"/>
               </zeroOrMore>
            </list>
         </element>
      </element>
   </start>

</grammar>

5. Download

This zip file contains examples and an XSLT stylesheet for the conversion.

6. Caveats

  1. Complex types of XML Schema can be mimicked but simple types cannot.
  2. derivation-by-restiction of XML Schema cannot be mimicked.
  3. Class names cannot be namespace qualified.
  4. Subclasses cannot override declarations in superclasses, but merely reference to them. For example, you cannot make a siamese subclass of cat with colors restricted to "sealPoint" and "bluePoint".

Acknowledgement

I am grateful to Josh Lubell and John Cowan for their helpful comments.


$Revision: 1.8 $ $Date: 2002/07/16 17:22:05 $ by $Author: makoto $