title: Resolving type definitions from imported schema in XJC fails
body: <p>I've got this API using JAXB to conveniently use object models, generated from XML Schemas by the XJC (XML-to-Java) compiler, through named references. It abstracts the creation of JAXB contexts and finding ObjectFactory methods away by all sorts of background magic and reflection. The basic gist of it is that you'd always define one general schema, and then any number (may also be 0) of schemas "extending" that general one, each resulting in its own data model. The general schema carries the reusable definitions, the ones extending it use those to compose their own models.</p>
  
  <p>I've now run into the situation where I'd like to reuse the general schema for more than one project. The general type definitions should remain the same across projects, and some code will be built against the abstract classes generated from those. So I'd need to first generate classes for some generic schema, then generate those extending and using them separately. I'm using Maven for my build process.</p>
  
  <p>The problem I'm running into is resolving type definitions from that generic schema in the extending schemas.</p>
  
  <p>Suppose my generic schema is named "general.xsd" and looks like this:</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.foobar.com/general"
  xmlns:gen="http://www.foobar.com/general"
  elementFormDefault="qualified" attributeFormDefault="qualified"&gt;
  
      &lt;!-- Element (will usually be root) --&gt;
      &lt;xs:element name="transmission" type="gen:Transmission" /&gt;
  
      &lt;!-- Definition --&gt;
      &lt;xs:complexType name="Transmission" abstract="true"&gt;
          &lt;xs:sequence&gt;
              &lt;!-- Generic parts of a transmission would be in here... --&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Next to that there's a bindings file to do some naming customization and set the package name for the output:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
      version="2.1"&gt;
  
      &lt;!-- Bindings for the general schema --&gt;
      &lt;bindings schemaLocation="general.xsd" node="/xs:schema"&gt;
  
          &lt;schemaBindings&gt;
              &lt;package name="com.foobar.models.general"/&gt;
          &lt;/schemaBindings&gt;
  
          &lt;bindings node="//xs:complexType[@name='Transmission']"&gt;
              &lt;!-- Some customization of property names here... --&gt;
          &lt;/bindings&gt;
  
  &lt;/bindings&gt;
  </code></pre>
  
  <p>I'd then have the next bit in the POM of that project to generate the Java classes:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;plugin&gt;
      &lt;groupId&gt;org.jvnet.jaxb2.maven2&lt;/groupId&gt;
      &lt;artifactId&gt;maven-jaxb21-plugin&lt;/artifactId&gt;
      &lt;version&gt;0.8.0&lt;/version&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;id&gt;xjc-generate&lt;/id&gt;
              &lt;goals&gt;
                  &lt;goal&gt;generate&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;configuration&gt;
                  &lt;schemaDirectory&gt;${basedir}/src/main/resources/com/foobar/schemas&lt;/schemaDirectory&gt;
                  &lt;schemaLanguage&gt;XMLSCHEMA&lt;/schemaLanguage&gt;
                  &lt;addCompileSourceRoot&gt;true&lt;/addCompileSourceRoot&gt;
                  &lt;episode&gt;true&lt;/episode&gt;
                  &lt;removeOldOutput&gt;true&lt;/removeOldOutput&gt;
              &lt;/configuration&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>As you can see, I'm using the JAXB2.1 Maven plugin. I've set the option to have an episode file generated for step-wise compilation. The option to remove previous output was for a bug workaround; all it does is make sure everything's cleaned up first so recompilation is forced.</p>
  
  <p>So far so good. That project compiles without a hitch. It should be noted that apart from the generated Java classes, I also package the schemas into the resulting jar file. So those are available on the classpath! The <code>sun-jaxb.episode</code> file is in the META-INF, as it should be.</p>
  
  <p>Then I start on the project that uses schemas which will extend the above, by first importing it. One of the "subtypes" could look like this (I'll call it sub.xsd):</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.foobar.com/sub"
  xmlns:sub="http://www.foobar.com/sub"
  xmlns:gen="http://www.foobar.com/general"
  elementFormDefault="qualified" attributeFormDefault="qualified"&gt;
  
      &lt;xs:import namespace="http://www.foobar.com/general" /&gt;
  
      &lt;!-- Definition --&gt;
      &lt;xs:complexType name="SubTransmission"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="gen:Transmission"&gt;
                  &lt;xs:sequence&gt;
                      &lt;!-- Additional elements placed here... --&gt;
                  &lt;/xs:sequence&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Again, there's a bindings file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
      version="2.1"&gt;
  
      &lt;!-- Bindings for sub type --&gt;
      &lt;bindings schemaLocation="sub.xsd" node="/xs:schema"&gt;
  
          &lt;schemaBindings&gt;
              &lt;package name="com.foobar.models.sub"/&gt;
          &lt;/schemaBindings&gt;
  
      &lt;/bindings&gt;
  
  &lt;/bindings&gt;
  </code></pre>
  
  <p>And here's the bit from the POM of this project that takes care of the XJC generation:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;plugin&gt;
      &lt;groupId&gt;org.jvnet.jaxb2.maven2&lt;/groupId&gt;
      &lt;artifactId&gt;maven-jaxb21-plugin&lt;/artifactId&gt;
      &lt;version&gt;0.8.0&lt;/version&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;id&gt;xjc-generate&lt;/id&gt;
              &lt;goals&gt;
                  &lt;goal&gt;generate&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;configuration&gt;
                  &lt;schemaDirectory&gt;${basedir}/src/main/resources/com/foobar/schemas&lt;/schemaDirectory&gt;
                  &lt;schemaLanguage&gt;XMLSCHEMA&lt;/schemaLanguage&gt;
                  &lt;addCompileSourceRoot&gt;true&lt;/addCompileSourceRoot&gt;
                  &lt;episode&gt;false&lt;/episode&gt;
                  &lt;catalog&gt;${basedir}/src/main/resources/com/foobar/schemas/catalog.cat&lt;/catalog&gt;
                  &lt;episodes&gt;
                      &lt;episode&gt;
                          &lt;groupId&gt;com.foobar&lt;/groupId&gt;
                          &lt;artifactId&gt;foobar-general-models&lt;/artifactId&gt;
                          &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
                          &lt;scope&gt;compile&lt;/scope&gt;
                      &lt;/episode&gt;
                  &lt;/episodes&gt;
                  &lt;removeOldOutput&gt;true&lt;/removeOldOutput&gt;
              &lt;/configuration&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>Originally, all the schemas were in a single folder and I had the <code>schemaLocation</code> attribute in the import set to <code>general.xsd</code>, which worked fine. But now that things are separated across projects, I run into problems. The first issue was that the other schema could not be found. I've resolved this by taking the <code>schemaLocation</code> attribute out of the <code>&lt;xs:import /&gt;</code> element, keep only the <code>namespace</code> attribute and adding a catalog file (<code>catalog.cat</code>) which you can see referenced in the above POM extract. Its contents are:</p>
  
  <pre class="lang-xml prettyprint-override"><code>PUBLIC "http://www.foobar.com/general" "classpath:/com/foobar/schemas/general.xsd"
  </code></pre>
  
  <p>This seems to work, since I no longer get an error that states the schema cannot be found. But for some reason, resolving the actual type definitions from the imported schema continues to fail. Here's the exception:</p>
  
  <pre class="lang-xml prettyprint-override"><code>Error while parsing schema(s).Location [ file:/C:/NetBeans_groups/Test/SubModelBundle/src/main/resources/com/foobar/schemas/sub.xsd{...,...}].
  org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'gen:Transmission' to a(n) 'type definition' component.
  </code></pre>
  
  <p>Here's what I tried so far:</p>
  
  <ul>
  <li>Use a catalog file. Partially successful, since the imported schema can now be found.</li>
  <li>Have the compilation for the general schema generate an episode file and use this for the compilation of the sub schema. Doesn't appear to make a difference, although this should only play a role once the type was resolved, so I don't think this is important yet.</li>
  <li>Use a different JAXP (note: <strong>not</strong> JAXB, JAXP) implementation. It did use a different one, because I could see that in the exception's stack trace, but the end result is the same.</li>
  <li>Use the <code>maven-jaxb22-plugin</code> instead of 21. No difference.</li>
  </ul>
  
  <p>Looking around online, it seems people have been running into this issue since at least 2006 and it might be related to some Xerces resolver problems. I hope that this is not some bug that's been lurking around for 6 years without anyone caring to fix it. Does someone else have some suggestions? Maybe someone ran into the same problem and found a solution? The only workaround I can think of is to use 'svn:externals' to drag the general schema into the sub project and just regenerate the classes there, but it's dirty and will only work when you can connect to our svn repo.</p>
  
  <p>Much thanks in advance for reading this long post. Do keep in mind that I've taken all of the above from existing projects and replaced some namespaces and other things for anonymity, so some typos are possible.</p>