MURATA Makoto (eb2m-mrt@asahi-net.or.jp)
Copyright © 2002 MURATA Makoto
Distributed under GPL
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.
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.
Although I retain the copyrights for the XSLT stylesheet, I claim no rights in grammars generated by the XSLT stylesheet.
<?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>
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>
.
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.
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>
This zip file contains examples and an XSLT stylesheet for the conversion.
siamese
subclass of cat
with colors
restricted to "sealPoint" and "bluePoint".I am grateful to Josh Lubell and John Cowan for their helpful comments.