I think your problem is here:
<mapping type-name="Container" abstract="true"
class="org.standard.Container">
<structure name="Header" field="header" map-as="Header" usage="optional"/>
...
</mapping>
<mapping name="Container"
class="org.standard.Container">
<structure map-as="Container"/>
</mapping>
You're telling Jibx that Container includes a Container element as it's
first element. In general, you don't have to "double-declare" elements
this way unless you're extending from something that can appear as a
standalone element (such as your "Header" example).
I took this out and made a few other minor changes to your binding and,
assuming that I understand your class structure and XML structure
correctly, was able to produce a binding file that works correctly:
<binding direction="input">
<namespace uri="http://www.gmail.com/st" prefix="st"
default="elements" />
<namespace uri="http://www.gmail.com/my" prefix="my" />
<mapping class="org.standard.SuperHeader" abstract="true"
ns="http://www.gmail.com/st">
<value name="companyId" field="companyId" />
</mapping>
<mapping class="org.standard.Header" ns="http://www.gmail.com/st"
type-name="header" abstract="true">
<structure map-as="org.standard.SuperHeader" />
<value name="branchId" field="branchId" />
</mapping>
<mapping name="Header" class="org.standard.Header"
extends="org.standard.SuperHeader"
ns="http://www.gmail.com/st">
<structure map-as="header" />
</mapping>
<mapping name="myHeader" class="com.my.Header"
extends="org.standard.Header" ns="http://www.gmail.com/my">
<structure map-as="header" />
<value name="employeeId" field="employeeId"
ns="http://www.gmail.com/my" />
</mapping>
<mapping name="Container" class="org.standard.Container"
ns="http://www.gmail.com/st">
<structure field="header" />
</mapping>
</binding>
I added some random data elements to each of the classes so that I could
verify that the bindings were being processed correctly.
I tested this against the following two XML documents:
<st:Container xmlns:st="http://www.gmail.com/st">
<my:myHeader xmlns:my="http://www.gmail.com/my">
<st:companyId>123</st:companyId>
<st:branchId>456</st:branchId>
<my:employeeId>abc</my:employeeId>
</my:myHeader>
</st:Container>
and:
<st:Container xmlns:st="http://www.gmail.com/st">
<st:Header>
<st:companyId>123</st:companyId>
<st:branchId>456</st:branchId>
</st:Header>
</st:Container>
Which I beleive encompass your primary use cases (again, data elements
made up so I could verify that everything unmarshalled correctly).
I take it from your description below that myHeader extends Header,
which extends SuperHeader (and Container extends nothing, but has a data
element of type SuperHeader). If I'm off about the actual type
hierarchy, things change around a bit, but not much.
On Wed, 2008-01-23 at 17:52 -0500, amphoras wrote:
> Hi,
>
> First, I want to say that JiBX is a really neat tool. It makes XML to
> Java serialization so easy. However, we are using schema group
> substitution a lot, and this seems to be an "advanced" feature. I
> would really appreciate your help.
>
> I have read the page about mapping inheritance
> (http://jibx.sourceforge.net/tutorial/binding-mappings.html#inherit)
> many times, but I am still not able to get it to work. What I have is
> this class hierarchy:
>
> org.standard.SuperHeader
> org.standard.Header
> com.my.Header
>
> The schema for my Header says (st=standard):
> <xsd:element name="Header" type="HeaderType" substitutionGroup="st:Header" />
>
> The JiBX mapping is defined as follows:
> <mapping type-name="Header" abstract="true"
> class="org.standard.Header"
> extends="org.standard.SuperHeader">
> <structure map-as="SuperHeader"/>
> ...
> </mapping>
>
> <mapping name="Header"
> class="org.standard.Header"
> extends="org.standard.SuperHeader">
> <structure map-as="Header"/>
> </mapping>
>
>
> subclass:
> <mapping name="myHeader"
> class="com.my.Header"
> extends="org.standard.Header">
> <structure map-as="Header"/>
> ...
> </mapping>
>
> Then I have a container class, let's call "org.standard.Container"
> that can contain either "org.standard.Header" or "com.my.Header":
>
> <mapping type-name="Container" abstract="true"
> class="org.standard.Container">
> <structure name="Header" field="header" map-as="Header" usage="optional"/>
> ...
> </mapping>
>
> <mapping name="Container"
> class="org.standard.Container">
> <structure map-as="Container"/>
> </mapping>
>
> The Java class is like:
>
> package org.standard;
>
> public class Container {
> Header header;
> }
>
> When I process XML that contains <st:Header>, everything's fine. But
> when I process XML that contains <my:Header>, it doesn't work:
>
> Here's the XML:
> <st:Container>
> <my:Header>
> ...
> </my:Header>
> </st:Container>
>
> I get:
>
> org.jibx.runtime.JiBXException: Expected "{http://standard}Container"
> end tag, found "{http://my}Header" start tag (line 67, col 35)
> at
> org.jibx.runtime.impl.UnmarshallingContext.parsePastCurrentEndTag(Unknown
> Source)
>
> I have tried all different combinations of "map-as" to no avail. I
> really like the natural, OO way that JiBX does schema group
> substitution, so I really want to get this to work. I appreciate any
> help that you can give me.
>
> Thanks!
> --Polly
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> jibx-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jibx-users
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users