Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Java classes from multiple XSDs with XJC

Tags:

java

jaxb

xjc

I have two xsd files:

base.xsd:

<schema
  targetNamespace="http://www.myorg.com/base"
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"
  xmlns="http://www.w3.org/2001/XMLSchema">
...
<complexType name="NrmClass">
    ...
</complexType>
...
</schema>

main.xsd is a schema where we want to use a type from base.xsd

<schema
  targetNamespace="http://www.myorg.com/main"
  elementFormDefault="qualified"
  xmlns="http://www.w3.org/2001/XMLSchema"
  xmlns:xn="http://www.myorg.com/base">

<import namespace="http://www.myorg.com/base"/>
...
<element>
  <complexType>
    <complexContent>
      <extension base="xn:NrmClass">
...

      </extension>
    </complexContent>
  </complexType>
</element>
...
</schema>

When I try to compile both, I receive the following error:

> xjc base.xsd main.xsd
parsing a schema...
[ERROR] src-resolve: Cannot resolve the name 'xn:NrmClass' to a(n) 'type definition' component.
  line 48 of file:/main.xsd

What is wrong here?

like image 493
kavai77 Avatar asked May 10 '12 10:05

kavai77


2 Answers

You want to try specifying the file for the XSD you're importing, as in:

<xsd:import namespace="http://www.myorg.com/base" schemaLocation="base.xsd"/>

This works well if you keep them side by side anyway. That way you can compile them in one operation.

If you want to run xjc separately (like they are built as separate modules), then you can use an episode file.

like image 81
Patrice M. Avatar answered Sep 21 '22 14:09

Patrice M.


Have a look at "episodes": http://weblogs.java.net/blog/kohsuke/archive/2006/09/separate_compil.html

like image 39
Puce Avatar answered Sep 20 '22 14:09

Puce