简体   繁体   中英

jaxb xsd schemas reusing

Let's assume I have three xsd schemas: CommonSchema.xsd, A.xsd, B.xsd. In CommonSchema.xsd I have some elements which I want to reuse in A.xsd and B.xsd. And of cause I want to reuse code generated by means org.jvnet.jaxb2.maven2 plugin. I want to get something like this in generated java code:

A extends CommonSchema {...}
B extends CommonSchema {...}

Or private reference from A.java and B.java to CommonSchema.java

What is the best approach to do it? Thanks in advance

You write the common XML Schema "com.xsd" as usual. Say it contains a complex type TypeCommon and its target namespace is "a:b". Then, another XML Schema file using stuff from "com.xsd" would use s.th. like

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:ns="a:b"
           targetNamespace="x:y">

    <xs:import schemaLocation="com.xsd" namespace="a:b"/>

    <xs:complexType name="TypeX2">
      <xs:sequence>
        <xs:element name="name" type="ns:TypeCommon"/>
      </xs:sequence>
    </xs:complexType>
</xs:schema>

A class hierarchy results from complex type extension, using eg

<xs:complexType name="TypeA">
  <xs:complexContent>
    <xs:extension base="ns:TypeCommon">
    <xs:sequence>
      <xs:element name="foo" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="bar" type="xs:int" fixed="1"/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM