Arseny Istlentyev
Arseny Istlentyev

Reputation: 449

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

Upvotes: 0

Views: 267

Answers (1)

laune
laune

Reputation: 31290

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 e.g.

<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>

Upvotes: 1

Related Questions