title: How to generate @XmlRootElement Classes for Base Types in XSD?
body: <p>I am having some issues with generating Java Classes with appropriate JAXB annotations from an XSD using XJC.</p>
  
  <p>I have a relatively simple XSD file defining my XML schema. The complex types within the XSD take advantage of inheritance using the <code>&lt;xs:extension&gt;</code> tags. The problem I having is that I need all complex types to generate Java Classes with the <code>@XmlRootElement</code>. </p>
  
  <p>Unfortunately, the way in which XJC generates the classes means that only derived class gets the <code>@XmlRootElement</code> (not the base class). I am using the simple global binding directive to ensure that it solves many of the other issues that I have faced with XJC. </p>
  
  <p>Here is an example snippet of the XSD:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xs:schema version="1.0" targetNamespace="http://www.knowledgemill.com/kmcs"
    xmlns:kmcs="http://www.knowledgemill.com/kmcs"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.0"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc"
    elementFormDefault="qualified"&gt;
      &lt;xs:annotation&gt;
          &lt;xs:appinfo&gt;
              &lt;jaxb:globalBindings&gt;
                  &lt;xjc:simple /&gt;
              &lt;/jaxb:globalBindings&gt;
          &lt;/xs:appinfo&gt;
      &lt;/xs:annotation&gt;
  
      &lt;xs:element name="Artifact" type="kmcs:Artifact"/&gt;
      &lt;xs:element name="EmailArtifact" type="kmcs:EmailArtifact"/&gt;
  
      &lt;xs:complexType name="Artifact"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="artifactId" type="xs:string" minOccurs="0"/&gt;
              &lt;xs:element name="artifactType" type="xs:string" minOccurs="0"/&gt;
              &lt;xs:element name="contentHash" type="xs:string" minOccurs="0"/&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="EmailArtifact"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="kmcs:Artifact"&gt;
                  &lt;xs:sequence&gt;
                      &lt;xs:element name="subject" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="threadSubject" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="from" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="to" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="cc" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="bcc" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="messageId" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="date" type="xs:date" minOccurs="0"/&gt;
                      &lt;xs:element name="size" type="xs:long" minOccurs="0"/&gt;
                      &lt;xs:element name="hasAttachment" type="xs:boolean" minOccurs="0"/&gt;
                      &lt;xs:element name="sensitivity" type="xs:string" minOccurs="0"/&gt;
                      &lt;xs:element name="headerHash" type="xs:string" minOccurs="0"/&gt;
                  &lt;/xs:sequence&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>As we can see from the above snippet, <code>EmailArtifact</code> extends <code>Artifact</code>. </p>
  
  <p>The java class code for <code>EmailArtifact</code> contains the following:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "EmailArtifact", propOrder = {
      "subject",
      "threadSubject",
      "from",
      "to",
      "cc",
      "bcc",
      "messageId",
      "date",
      "size",
      "hasAttachment",
      "sensitivity",
      "headerHash"
  })
  @XmlSeeAlso({
      ExtendedEmail.class
  })
  @XmlRootElement(name = "EmailArtifact")
  public class EmailArtifact
      extends Artifact
  {
  
      protected String subject;
      protected String threadSubject;
      protected String from;
      protected String to;
      protected String cc;
      protected String bcc;
      protected String messageId;
      @XmlSchemaType(name = "date")
      protected XMLGregorianCalendar date;
      protected Long size;
      protected Boolean hasAttachment;
      protected String sensitivity;
      protected String headerHash;
  </code></pre>
  
  <p>The java class code for <code>Artifact</code> contains the following:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "Artifact", propOrder = {
      "artifactId",
      "artifactType",
      "contentHash"
  })
  @XmlSeeAlso({
      ManagedDocArtifact.class,
      EmailArtifact.class
  })
  public class Artifact {
  
      protected String artifactId;
      protected String artifactType;
      protected String contentHash;
  </code></pre>
  
  <p>In the <code>EmailArtifact</code> we can see that it contains the <code>@XmlRootElement</code> but the base type <code>Artifact</code> does not contain <code>@XmlRootElement</code>. </p>
  
  <p>How can you force XJC to generate <code>@XmlRootElement</code> for all classes including the base types. </p>