users@jaxb.java.net

Re: subclasses get xsi:type instead of real type

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 20 May 2009 14:43:34 +0200

The schema snippet you posted isn't quite correct, and it does not match the
XML as shown; and even if the <Events> is added, it cannot be validated,
because <event> cannot be omitted for a State type.

To answer your question: yes, this is to be expected, and it's due to using
a class and its subclass with the same tag (<ScheduleEvent>).

As a remedy, consider using a <choice> between two distictly tagged
elements, one for a ScheduleType, the other one for a State, like this:

    <xs:sequence>
      <xs:element minOccurs="0" name="Events">
      <xs:complexType>
        <xs:choice maxOccurs="unbounded">
          <xs:element name="ScheduleEvent" type="ScheduleType"/>
          <xs:element name="State" type="State"/>
        </xs:choice>
      </xs:complexType>
      </xs:element>
    </xs:sequence>

-W


On Wed, May 20, 2009 at 10:22 AM, <christer.larsson_at_seb.se> wrote:

> Probably been asked before but I can't find a suitable answer to a
> problem I am facing.
>
> I have the following schema:
>
> <xs:complexType name=*"Schedule"*>
> <xs:complexContent>
> <xs:sequence>
> <xs:element minOccurs=*"0"* name=*"Events"*>
> <xs:complexType>
> <xs:sequence>
> <xs:element maxOccurs=*"unbounded"* name=*"ScheduleEvent"*
> type=*"ScheduleType"*/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:sequence>
> </xs:complexContent>
> </xs:complexType>
>
> <xs:complexType name=*"ScheduleType"*>
> <xs:sequence>
> <xs:element name=*"event"* type=*"xs:string"*/>
> </xs:sequence>
> </xs:complexType>
>
> <xs:complexType name=*"State"*>
> <xs:complexContent>
> <xs:extension base=*"ScheduleType"*>
> <xs:sequence>
> <xs:element name=*"secondEvent"* type=*"
> xs:string"*/>
> </xs:sequence>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
>
> When adding a State to Schedule and marshalling the message I get the
> following output:
>
> <Schedule>
> <ScheduleEvent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:type="ns0:State">
> <***secondEvent*>test</***secondEvent*>
> </ScheduleEvent>
> </Schedule>
>
> I was expecting:
>
> <Schedule>
> <State>
> <secondEvent>test</secondEvent>
> </State>
> <Schedule>
>
> Is this the expected behaviour?
>
>
>
>