I have an interesting XML situation in which I’m trying to write an XSD for. The web service I am using has an unusual way of sending responses in that it is generic in a way.
For example,
Web Service Call 1:
<rootElement>
<result>
<resultset>
<row attr="some value" attr2="some value 2" />
</resultset>
</result>
</rootElement>
Web Service Call 2:
<rootElement>
<result>
<resultset>
<row someOtherAttr="some value" someOtherAttr2="some value 2" />
</resultset>
</result>
</rootElement>
As you can see, the only thing that differentiates the two web service responses is the attributes within the row element.
I’ve tried a few different approaches by having an abstract element for the result element, which works to a point, but I can’t figure out how allow JAXB to choose which result element to use when marshalling.
i.e.
... other xs declarations excluded for sanity ...
<xs:element ref="abstractResult" />
... snip ...
<xs:element name="abstractResult" type="ResultType" abstract="true" />
<xs:complexType name="ResultType" abstract="true" />
<xs:complexType name="SomeResultType">
<xs:complexContent>
<xs:extension base="ResultType">
... snip ...
The problem with this approach is JAXB tries to instantiate the abstract ResultType while marshalling rather than searching for a proper implementation.
Is there any way to avoid this?
Thanks a bunch!
Ended up separating the schemas into independent files in which there are the different implementations of the root element.
I then tied the multiple schemas to their own namespace so the marshaller is able to differentiate between the similar elements.
Since the xml comes in without a namespace from the webservice, I am using JDom to set the namespace since I know what the target object type will be at runtime.
Hope this helps anyone interested.