XML schema for representing objects (inheritance et al)

Forgive me if this topic has been discussed before - I searched far
and wide on Google but came up with nothing.

I am developing an XML schema to store information about objects,
whereby "objects" I mean roughly "objects in OOP", minus the methods.
I need to support inheritance, and roughly the equivalent of constant
static data members in each object.

The schema I've developed so far works, but it is *far* from ideal.
It permits me to express objects in an XML document exactly how
I want to, e.g. "Objects.xml" :

----------

<?xml version="1.0" encoding="utf-8" ?>
<Objects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation='Objects.xsd'
         my_UUID="368eef11-06a2-456c-ac91-5949edaa6722">

  <Person my_UUID="25c007fc-72fe-41ae-acce-d662b705c0cc">
    <SomeField>148ab</SomeField>
    <d_Name>James Paul Tipkin</d_Name>
    <d_PhoneNumber>(800)555-1212</d_PhoneNumber>
  </Person>

  <Musician my_UUID="0cff5934-8f4e-45c7-a2dd-d217a655def2">
    <SomeField>274cf</SomeField>
    <d_Name>Ringo Starr</d_Name>
    <d_PhoneNumber>(800)555-1213</d_PhoneNumber>
    <d_BandName>Beatles</d_BandName>
  </Musician>

</Objects>

----------

The my_UUID fields are used to uniquely identify the instance
document, and each object (across all instance documents.)

I've included the schema I've developed below.  Let me make a
few notes first.

Object types are created using complexTypes.

I want constant static data members so that every instance of
an object will have some basic information about that type of
object.  For exampe, like objects, each object _type_ has a
UUID associated with it.  Given some object in the XML file,
I'd like to be able to find out what the UUID of its type is.
I don't want to have this information cluttering the instance
document.  I've done this using "default" attributes.  This is
not ideal because the XML instance document must not be able to
change this information - it's the same across every instance
of an object.

That said, now the schema ("Objects.xsd"):

----------

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

  <!-- Object types -->

  <xs:element name="_Object" />

  <xs:complexType name="_Object">
    <xs:sequence>
      <xs:element name="SomeField" type="xs:string" minOccurs="1" 
maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="my_UUID" type="xs:string" use="required" />
    <xs:attribute name="myType_UUID" type="xs:string" />
  </xs:complexType>

  <xs:complexType name="t_Person-mid">
    <xs:complexContent>
      <xs:restriction base="_Object">
        <xs:sequence>
          <xs:element name="SomeField" type="xs:string" minOccurs="1" 
maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="my_UUID" type="xs:string" use="required" />
        <xs:attribute name="myType_UUID" ype="xs:string"
                      default="4584bf3d-8ccc-4061-aa8a-7d98c4009b8a" />
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="t_Person">
    <xs:complexContent>
      <xs:extension base="t_Person-mid">
        <xs:sequence>
          <xs:element name="d_Name" type="xs:string" />
          <xs:element name="d_PhoneNumber" type="xs:string" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="t_Musician-mid">
    <xs:complexContent>
      <xs:restriction base="t_Person">
        <xs:sequence>
          <xs:element name="SomeField" type="xs:string" minOccurs="1" 
maxOccurs="unbounded" />
          <xs:element name="d_Name" type="xs:string" />
          <xs:element name="d_PhoneNumber" type="xs:string" />
        </xs:sequence>
        <xs:attribute name="my_UUID" type="xs:string" use="required" />
        <xs:attribute name="myType_UUID" type="xs:string"
                      default="8a37a88b-df1f-410e-a534-6e1be018a02e" />
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="t_Musician">
    <xs:complexContent>
      <xs:extension base="t_Musician-mid">
        <xs:sequence>
          <xs:element name="d_BandName" type="xs:string" />
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:element name="Person" type="t_Person" substitutionGroup="_Object" />
  <xs:element name="Musician" type="t_Musician" substitutionGroup="_Object" 
/>

  <!-- The document -->

  <xs:element name="Objects">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="_Object" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attribute name="my_UUID" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>


</xs:schema>

----------

Is that sufficiently ugly?  =)

Is there a better way?

I'm facing these other things:  (I should note that I'm using
Xerces 2 for Java):

1. Because I have to restrict the myType_UUID fields (so that they
   are some particular value) I have to do a restriction "pass" in
   addition to the extension one.  In the restriction, I have to
   list _every_ field, even those I don't need to mess with.  (You
   can imagine how bad this will get if objects need many fields,
   and for my project, they most definitely will.)  Why even atempt
   an OO solution if I have to do this?

2. This is also a problem, because for some fields, I may want to
   indicate a field's default or fixed value, that will remain that
   field's default or fixed value, even if the class is inherited.
   I then want an inheriting class to be able to re-define it to
   optional, fixed, default - whatever it wants.

2. As I said before, I have to use "default" values for those
   UUIDs.  I'd prefer to use "fixed" (so they can't be changed in
   an instance document,) but I get all kinds of errors when I
   try to change it to fixed.

3. I'd like to define myType_UUID in "_Object" as "required", so that
   an error is generated if any sub-classes don't define it.  But
   then I get into problems when I change it from required to
   "default."

Is my solution even close to ideal?  Is there some kind of "standard"
for reprsenting things like this?

Thanks so much,

Ishmael Yavitz

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx

Received on Monday, 22 July 2002 19:58:22 UTC