users@jaxb.java.net

RE: Re: subclasses get xsi:type instead of real type

From: <christer.larsson_at_seb.se>
Date: Wed, 20 May 2009 14:57:14 +0200

Hi
 
Sorry for the typo.
 
But great and thank you very much for the working fix
 
 
/Christer

________________________________

From: Wolfgang Laun [mailto:wolfgang.laun_at_gmail.com]
Sent: den 20 maj 2009 14:44
To: users_at_jaxb.dev.java.net
Subject: Re: subclasses get xsi:type instead of real type


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?