开发者

JAXB2 map xsd:restrictions as subclasses and (un)marshall xsi:types

I have been doing a lot of research trying to figure this out, but am still unsuccessful.

I have a number of XSD that follow this schema:

Simple_Identification.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <xsd:include schemaLocation="./Base_3039.xsd"/>
       <xsd:include schemaLocation="./Simple_A.xsd"/>
       <xsd:include schemaLocation="./Simple_S.xsd"/>
       <xsd:include schemaLocation="./Simple_N.xsd"/>
       <xsd:include schemaLocation="./Simple_V1.xsd"/>
       <xsd:include schemaLocation="./Simple_L.xsd"/>
       <xsd:include sc开发者_开发知识库hemaLocation="./Simple_V.xsd"/>
       <xsd:include schemaLocation="./Simple_C.xsd"/>
       <xsd:simpleType name="Simple_Identification">
               <xsd:restriction base="Base_3039"/>
       </xsd:simpleType>
</xsd:schema>

where for example Simple_S.xsd looks like this:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:include schemaLocation="./Simple_Identification.xsd"/>
    <xsd:simpleType name="Simple_S">
      <xsd:restriction base="Simple_Identification">
           <xsd:minLength value="14"/>
           <xsd:maxLength value="14"/>
     </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

Eventually, I would like to be able to generate XML files that contain tags looking like this:

<Identification xsi:type="Simple_S">XYZUVW</Identification>

For now, without enabling mapSimpleTypeDef, I am able to marshall/unmarshall XML files, ignoring the simple types like Simple_S.

After enabling mapSimpleTypeDef classes are generated for the simple types. Simple_Identification is mapped to a class containing a Base_3039 field. And Base_3039 class contains a String field. However, the classes for the different subtypes of Simple_Identifications do not extend Simple_Identification but only contain a field of type Simple_Identification, which does not help when marshalling/unmarshalling.

For example, when unmarshalling this XML file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="...">
  <Identification>EDS-200708021031-950012222329</Identification>
  <Time>2007-08-02T10:31:44.449+01:00</Time>
  <Function>9</Function>
  <Sender>
    <Identity xsi:type="Simple_S">111111380002111</Identity>
 </Sender>
</Header>

the value of Identity is unmashalled to a Simple_Identification object not to a specialized Simple_S object. Moreover, if I marshall back all xsi:type attributes are lost in the generated XML.

So, basically, my question is how can I properly unmarshall and generate XMLs containing xsi:types. Are the schemas I'm working with not appropriate for doing this? Is xsd:restriction not interpreted as a type of inheritance by JAXB?

Note that the XSDs are not mine to modify, I just have to work with them to properly read and generate XMLs.

Thanks for taking the time to help me figure this out!

-Anca


JAXB does support xsi:type, but you will only see different sub-classes generated (and instance objects created) when dealing with complex types, not simple types.

Restricting a simple type using only a facet does not lead to the creation of a new class with JAXB. That is because, purely in object-oriented terms, the derived class would look exactly the same as the super-class anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜