title: Shared class for child element in JAXB in different xmls/roots
body: <p>In <strong>JAXB</strong> when using automatic class generation via <strong>xjc</strong> from <em>xsd</em> scheme.</p>
  
  <p><strong>alpha.xsd</strong></p>
  
  <pre><code>&lt;xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="alpha"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="persons"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                &lt;xs:element name="person" maxOccurs="unbounded" minOccurs="0"&gt;
                  &lt;xs:complexType&gt;
                    &lt;xs:sequence&gt;
                      &lt;xs:element type="xs:string" name="name"/&gt;
                    &lt;/xs:sequence&gt;
                  &lt;/xs:complexType&gt;
                &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p><strong>beta.xml</strong></p>
  
  <pre><code>&lt;xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="country"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="class"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                &lt;xs:element name="person"&gt;
                  &lt;xs:complexType&gt;
                    &lt;xs:sequence&gt;
                      &lt;xs:element type="xs:string" name="name"/&gt;
                    &lt;/xs:sequence&gt;
                  &lt;/xs:complexType&gt;
                &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>As you can see there is <code>Person</code> element which is shared among these two schemes. What I would like to do is:</p>
  
  <ul>
  <li>generate classes using xjc in a way that <code>ObjectFactory</code> class is shared for both schema classes (the output classes will be in one package)</li>
  <li>not use nested static classes (with attribute <code>localScoping="toplevel"</code>)</li>
  <li>use <code>Person</code> class to bind with <code>/alpha/persons/person</code> as with <code>/country/class/person</code> so there are not two Person classes created</li>
  </ul>
  
  <p>The purpose of this is unmarshalling one xml, applying business logic and creating another one as output where some elements (like <code>Person</code>) are same and shared for both xml files. The namespace will be the same for both files.</p>
  
  <p>I would welcome if you can present me with complete .xjb bindings settings file. So far mine contains only:</p>
  
  <pre><code>&lt;jxb:bindings version="1.0" 
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    jxb:extensionBindingPrefixes="xjc"&gt;
  
    &lt;jxb:globalBindings localScoping="toplevel"/&gt;
  &lt;/jxb:bindings&gt;
  </code></pre>
  
  <p>And of course I get name collision error as I do not know how to set binding compiler to see <code>Person</code> as the same entity/element.</p>