users@jaxb.java.net

Re: problem marshlled elements when substitutionGroup defined

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Sat, 15 Nov 2008 21:41:29 +0100

The JAXBContext you are creating with
   JAXBContext.newInstance(ElementExt.class)
is too limited. If you use the package name
   JAXBContext.newInstance( "someuri" )
your unmarshalling example works fine.

Having @XmlRootElement for the class generated from an anonymous complex
type of a substitution group element is explicitly ruled out by the JAXB
Specification.

Wolfgang

On Sat, Nov 15, 2008 at 12:40 AM, Mike Skells <mike.skells_at_validsoft.com>wrote:

>
> Hi,
> I am ttrying to marshall/unmarshall elements that have a substitutionGroup
> defined
> It seems that when a top level element is defined with the
> substitutionGroup
> then JAXB doesnt define the @XmlRootElement and marshalling fails.
> If the substitutionGroup from the element-ext is removed then the
> annotation
> is inserted and all is fine, but I cant see a way to get XJC to treat this
> element as a top level element, and allow substitution
>
> I am using jaxb2.1.9
> the schema used is
>
> <xs:schema xmlns:local="someuri" xmlns:xs="
> http://www.w3.org/2001/XMLSchema"
> targetNamespace="someuri" elementFormDefault="qualified"
> attributeFormDefault="unqualified">
> <xs:complexType name="base" abstract="true">
> <xs:attribute name="attr1" type="xs:string" use="required"/>
> </xs:complexType>
> <xs:element name="element-base" type="local:base" abstract="true"/>
> <xs:element name="element-ext"
> substitutionGroup="local:element-base">
> <xs:complexType>
> <xs:complexContent>
> <xs:extension base="local:base">
> <xs:attribute name="attr3"
> type="xs:string" use="required"/>
> </xs:extension>
> </xs:complexContent>
> </xs:complexType>
> </xs:element>
> </xs:schema>
>
>

>
>
> it then it is unmarshalled like this
> JAXBContext context = JAXBContext.newInstance(ElementExt.class);
> Unmarshaller unmarshaller = context.createUnmarshaller();
>
> unmarshaller.unmarshal(new StringReader(
> "<local:element-ext attr1='S' attr3='s' xsi:schemaLocation='someuri
> test2.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
> xmlns:local='someuri'/>"));
>
> then I get the following exception
>
> Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected
> element (uri:"someuri", local:"element-ext"). Expected elements are (none)
> at
>
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:609)
>
>