users@jaxb.java.net

Re: XML element polymorphism??

From: Joe Fialli <Joseph.Fialli_at_sun.com>
Date: Tue, 03 Jun 2003 09:47:26 -0400

The next version of JAXB standard implementation supports element
substitution, which
can also be used to extend a schema using derivation by extension. The
syntax and mechanism
for element group substitution is slightly different than type
substitution, but you will be able
to achieve the flexibility you are seeking.

-Joe Fialli, Sun Microsystems

Filip Rindler wrote:

>>Filip Rindler <filip.rindler_at_gmx.de> wrote:
>>
>>
>>>This XML encoder mentioned above always polymorphically returns a
>>>
>>>
>XMLBMatrix
>
>
>>>element (that's the XMLB class corresponding to the Matrix element
>>>
>>>
>type),
>
>
>>>i.e. the runtime type is a subclass of XMLBMatrix. This works, because
>>>
>>>
>JAXB
>
>
>>>is as smart as to recognize the <xsd:extension base="Matrix"> tag and
>>>
>>>
>let
>
>
>>>XMLBMatrix2DGrid (XMLB class corresponding to the Matrix2DGrid element
>>>
>>>
>type)
>
>
>>>extend the XMLBMatrix class. In the schema above it might become clear,
>>>
>>>
>that
>
>
>>>the 'Matrix' element type could be called 'abstract', because it will
>>>
>>>
>never
>
>
>>>actually occur in the document, only its derived elements (such as
>>>Matrix2DGrid) will occur.
>>>
>>>
>>I'm bit confused since there's just that "Matrix" complex type, no
>>"Matrix" element. And I have no idea where XMLB comes from. Is it the
>>prefix you assigned? Plus what do you mean by "element type"? Is it
>>"element decl" or "complex type"?
>>
>>
>
>I guess I was somewhat confused myself when writing that ;-) What I meant is
>the following (second try): if I have an XML complex type called Matrix and
>some XML subtypes called Matrix2DGrid and Matrix3DGrid that both extend this
>complex type (using <xsd:extension base="Matrix">). Then in my root XML
>element (here mtxfile) I want to have only one <matrix> element that can
>take up any of the types extending the complex type 'Matrix'. I meant that
>when I was talking of 'polymorphism' which is the same in a programming
>language. So assuming Matrix2DGrid contains sub-elements 'width' and
>'height' and Matrix3DGrid contains 'width' and 'height' and 'depth' both the
>following XML files would (should) be valid:
>
>...
><matrix>
> <width>12</width>
> <height>13</height>
></matrix>
>
>and:
>
><matrix>
> <width>12</width>
> <height>13</height>
> <depth>23</depth>
></matrix>
>
>So the 'matrix' XML element (of type Matrix) would be able to take up all
>XML compley types extending 'Matrix' which then would be somewhat like its
>'superclass'.
>By the way, you were right in the assumption that XMLB is my assigned prefix
>(sorry I forgot that).
>
>
>
>>>OK, now after this long tale, here's my problem: While it seems to work
>>>
>>>
>to
>
>
>>>just assign a XMLBMatrix2DGrid object to the 'mtxfile' root element
>>>
>>>
>using
>
>
>>>its generated setMatrix() method, i get a validation error. So is there
>>>
>>>
>a
>
>
>>That's probably because you are trying to use the type substitution here
>>and it's one of the unsupported features in 1.0.
>>
>>
>
>too bad ;-) any ideas when it's coming, that seems like a really valuable
>feature
>
>
>
>>
>>
>>>I'm not stubborn on the stuff I've written so far and if there's another
>>>
>>>
>way
>
>
>>>of accomplishing my goal, I can change everything, just to have that
>>>
>>>
>said
>
>
>>>;-)
>>>
>>>
>>I think one possibility is to define your schema as:
>>
>><xsd:complexType name="MTXFile">
>> <xsd:sequence>
>> <xsd:element name="minVersion" type="xsd:int" />
>> <xsd:choice>
>> <xs:annotation><xsd:appinfo>
>> <jaxb:property name="Matrix" />
>> </xsd:appinfo></xsd:annotation>
>> <xsd:element name="matrix2DGrid" type="Matrix2DGrid" />
>> <xsd:element name="matrix3DGrid" type="Matrix3DGrid" />
>> ...
>> </xsd:choice>
>> </xsd:sequence>
>></xsd:complexType>
>>
>>The property customization will map the choice into a single property,
>>so you can just say:
>>
>> mtxFile.setMatrix( new Matrix3DGrid() );
>>
>>or
>>
>> mtxFile.setMatrix( new Matrix2DGrid() );
>>
>>
>>
>
>Thanks, that doesn't help my problem, though because setMatrix() then is
>overloaded and I also have to declare the choice list, which removes the
>possibility to extend the SCHEME at run time, at least in my app..
>
>
>Filip Rindler
>
>