1
我有一个有趣的XML情况,我试图为其编写XSD。我使用的Web服务以一种不寻常的方式发送响应,因为它在某种程度上是通用的。XSD元素定义具有相同的名称,但具有不同的属性
例如,
Web Service调用1:
<rootElement>
<result>
<resultset>
<row attr="some value" attr2="some value 2" />
</resultset>
</result>
</rootElement>
Web Service调用2:
<rootElement>
<result>
<resultset>
<row someOtherAttr="some value" someOtherAttr2="some value 2" />
</resultset>
</result>
</rootElement>
正如你所看到的,区分两个Web服务响应的唯一的事情是行元素内的属性。
我已经尝试了几种不同的方法,为结果元素提供了一个抽象元素,该元素适用于某个点,但我无法弄清楚如何让JAXB选择在编组时使用哪个结果元素。
即
... 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 ...
这种方法的问题是JAXB试图实例抽象与resultType在编组,而不是寻找一个合适的实现。
有什么办法可以避免这种情况?
非常感谢!