users@jaxb.java.net

Re: JAXB 2.0 substitutionGroup and common type

From: Joe Fialli <Joseph.Fialli_at_Sun.COM>
Date: Wed, 17 Aug 2005 14:28:20 -0400

Simon Hutchinson wrote:

> Hi,
>
> <xs:element name="elem1" type="xs:string"/>
>
> <xs:element name="elem2" substitutionGroup="elem1"/>
>
> <xs:element name="Test1">
>
> <xs:complexType>
>
> <xs:sequence>
>
> <xs:element name="fault" type="common:Fault" minOccurs="0"/>
>
> <xs:element ref="elem1">
>
> <xs:annotation>
>
> <xs:appinfo>
>
> <jxb:property name="element"/>
>
> </xs:appinfo>
>
> </xs:annotation>
>
> </xs:element>
>
> </xs:sequence>
>
> </xs:complexType>
>
> </xs:element>
>
> My schema (snippet shown above) compiles OK.
>
> However the addidtion of the substitutionGroup for “elem2” has raised
> an issue for me.
>
> A call to the getElement() property in the XJC generated class returns
> a javax.xml.bind.JAXBElement – I can subsequently call getValue() on
> this element to return the string. However I know that this element
> will only ever return a String type. Is there any way that I can force
> the compiler to allow getElement() to return a string as opposed to a
> JAXBElement?
>
Once an element is substitutable, the JAXB property for it has to be an
element,
not the element's value, which is java.lang.String for both elem1 and
elem2 as you
pointed out above. The element's name becomes a dynamic entity with
element substituton, not
a static entity that can be associated statically with the JAXB property
with an annotation such
as @XmlElement{name="elem1").
The JAXB marshaller would not be able to preserve whether the
element name was "elem1" or "elem2" w/o the JAXB property being bound to
JAXBElement.
Section 6.7.5 in JAXB 2.0 Specification[1] has more details on this.

As Kohsuke mentioned in his reply, a convenience method can also be
added to just get the
value for the JAXB property named "elem1", but both validation and
marshalling require
the JAXB property that returns a JAXBElement.

-Joe

[1] http://jcp.org/aboutJava/communityprocess/pr/jsr222/index.html

> Thanks
>
> Si
>
> --
> Merlin Information Systems Limited,
> Merlin House, Gawcott Road, Buckingham, United Kingdom. MK18 1TN
> Tel: +44 (0) 1280 824331 Fax: +44 (0) 1280 824112
> http://www.misgl.com <http://www.misgl.com/>
> Provider of IT Services and Online Portal Support Services.
> Confidentiality:
> The information contained in this email (including any attachments) is
> confidential and is intended solely for the use of the named
> addressee. Access, copying or re-use of the information in it by any
> other person is not authorised. If you are not the intended recipient,
> please notify us immediately by telephone or by e-mail to
> admin_at_misgl.com <mailto:admin_at_misgl.com>
> *** This mail has been scanned for viruses ***