title: already defined error when converting xsd to java
body: <p>I want to translate a complicated set of xsd files into Java/Hibernate entities.  Towards that end, 
  I downloaded version 2.2.7 of JAXB <a href="https://jaxb.java.net/" rel="nofollow">from this link</a>. I unzipped the file, opened cmd.exe, navigated to the directory of the create-marshal sample, ran ant compile to confirm everything works, then ran ant clean to eliminate the results to return everything to starting conditions.  </p>
  
  <p>The problem came when I tried to use a different xsd file as the input for the create-marshal sample.  Specifically, I am getting an error when the xsd file distinguishes between data structures by changing the value of an attribute of the same tag type.  The create-marshal sample gives the following error in that case:  </p>
  
  <blockquote>
    <p>[ERROR] 'POCD_MT000040.InfrastructureRoot.typeId' is already defined<br>
        [xjc]   line 54 of file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd</p>
  </blockquote>
  
  <p>The error repeats again and again, once for every time that the value of the attribute is re-assigned for a different data structure definition.  <strong>How can I resolve this error?</strong>  </p>
  
  <p>Here are the first few iterations of the data structure in the xsd file:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;xs:schema targetNamespace="urn:the-publisher:v3" 
    xmlns:mif="urn:the-publisher:v3/mif" 
    xmlns="urn:the-publisher:v3"  
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"  
    xmlns:sdtc="urn:the-publisher:sdtc"&gt;
  &lt;xs:annotation&gt;
      &lt;xs:documentation&gt;Manually edited to add Schema Extensions, July 2012&lt;/xs:documentation&gt;
      &lt;xs:documentation&gt;Generated using schema builder version 2.0. Stylesheets: RoseTreeToMIFStaticModel.xsl version: 1.1 StaticMifToXsd.xsl version 2.0&lt;/xs:documentation&gt;
  &lt;/xs:annotation&gt;
  &lt;xs:complexType name="POCD_MT000040.InfrastructureRoot.typeId"&gt;
      &lt;xs:complexContent&gt;
          &lt;xs:restriction base="II"&gt;
              &lt;xs:attribute name="root" type="uid" 
                use="required" 
                fixed="2.16.840.1.113883.1.3"/&gt;
              &lt;xs:attribute name="extension" type="st" 
                use="required"/&gt;
          &lt;/xs:restriction&gt;
      &lt;/xs:complexContent&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="POCD_MT000040.Act"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="realmCode" type="CS" 
            minOccurs="0" 
            maxOccurs="unbounded"/&gt;
          &lt;xs:element name="typeId"
            type="POCD_MT000040.InfrastructureRoot.typeId" 
            minOccurs="0"/&gt;
          &lt;xs:element name="templateId" type="II" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:element name="id" type="II" minOccurs="0" 
            maxOccurs="unbounded"/&gt;
          &lt;xs:element name="code" type="CD"/&gt;
          &lt;xs:element name="text" type="ED" minOccurs="0"/&gt;
          &lt;xs:element name="statusCode" type="CS" 
            minOccurs="0"/&gt;
          &lt;xs:element name="effectiveTime" type="IVL_TS" 
            minOccurs="0"/&gt;
          &lt;xs:element name="priorityCode" type="CE" 
            minOccurs="0"/&gt;
          &lt;xs:element name="languageCode" type="CS" 
            minOccurs="0"/&gt;
          &lt;xs:element name="subject" 
            type="POCD_MT000040.Subject" minOccurs="0"/&gt;
          &lt;xs:element name="specimen" 
            type="POCD_MT000040.Specimen" minOccurs="0" 
            maxOccurs="unbounded"/&gt;
          &lt;xs:element name="performer" 
            type="POCD_MT000040.Performer2" minOccurs="0" 
            maxOccurs="unbounded"/&gt;
          &lt;xs:element name="author" 
            type="POCD_MT000040.Author" minOccurs="0" 
            maxOccurs="unbounded"/&gt;
          &lt;xs:element name="informant" 
            type="POCD_MT000040.Informant12" 
            minOccurs="0" 
            maxOccurs="unbounded"/&gt;
          &lt;xs:element name="participant" 
            type="POCD_MT000040.Participant2" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:element name="entryRelationship" 
            type="POCD_MT000040.EntryRelationship" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:element name="reference" 
            type="POCD_MT000040.Reference" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:element name="precondition" 
            type="POCD_MT000040.Precondition" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
      &lt;/xs:sequence&gt;
      &lt;xs:attribute name="nullFlavor" 
            type="NullFlavor" use="optional"/&gt;
      &lt;xs:attribute name="classCode" 
            type="x_ActClassDocumentEntryAct" use="required"/&gt;
      &lt;xs:attribute name="moodCode" type="x_DocumentActMood" 
            use="required"/&gt;
      &lt;xs:attribute name="negationInd" type="bl" 
            use="optional"/&gt;
  &lt;/xs:complexType&gt;
  &lt;xs:complexType name="POCD_MT000040.AssignedAuthor"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:element name="realmCode" type="CS" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:element name="typeId" 
            type="POCD_MT000040.InfrastructureRoot.typeId" 
            minOccurs="0"/&gt;
          &lt;xs:element name="templateId" type="II" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:element name="id" type="II" 
            maxOccurs="unbounded"/&gt;
          &lt;xs:element name="code" type="CE" minOccurs="0"/&gt;
          &lt;xs:element name="addr" type="AD" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:element name="telecom" type="TEL" 
            minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;xs:choice&gt;
              &lt;xs:element name="assignedPerson" 
            type="POCD_MT000040.Person" minOccurs="0"/&gt;
              &lt;xs:element name="assignedAuthoringDevice" 
            type="POCD_MT000040.AuthoringDevice" 
            minOccurs="0"/&gt;
          &lt;/xs:choice&gt;
          &lt;xs:element name="representedOrganization" 
            type="POCD_MT000040.Organization" minOccurs="0"/&gt;
      &lt;/xs:sequence&gt;
      &lt;xs:attribute name="nullFlavor" 
            type="NullFlavor" use="optional"/&gt;
      &lt;xs:attribute name="classCode" 
            type="RoleClassAssignedEntity" 
            use="optional" fixed="ASSIGNED"/&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p>Here are the corresponding first few iterations of the error message:  </p>
  
  <blockquote>
    <p>Build sequence for target(s) `compile' is [compile]<br>
        Complete build sequence is [compile, javadoc, clean, run, ]<br>
        compile:<br>
        [echo] Compiling the schema...<br>
        [mkdir] Created dir: C:\Temp\jaxb\apps\create-marshal\gen-src<br>
        [mkdir] Created dir: C:\Temp\jaxb\apps\create-marshal\gen-src\primer\po<br>
        [xjc] build id of XJC is 2.2.7<br>
        [xjc] Checking timestamp of C:\Temp\jaxb\apps\create-marshal\POCD_MT000040_SDTC.xsd<br>
        [xjc] the last modified time of the inputs is  1409957672397<br>
        [xjc] the last modified time of the outputs is -9223372036854775808<br>
        [xjc] Compiling file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd<br>
        [xjc] [ERROR] 'POCD_MT000040.InfrastructureRoot.typeId' is already defined<br>
        [xjc]   line 54 of file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd<br>
        [xjc]<br>
        [xjc] [ERROR] (related to above error) the first definition appears here<br>
        [xjc]   line 46 of file:/C:/Temp/jaxb/apps/create-marshal/infrastructure/cda/POCD_MT000040_SDTC.xsd<br>
        [xjc]<br>
        [xjc] [ERROR] 'POCD_MT000040.Act' is already defined<br>
        [xjc]   line 81 of file:/C:/Temp/jaxb/apps/create-marshal/POCD_MT000040_SDTC.xsd<br>
        [xjc]   </p>
  </blockquote>
  
  <p>.....the same error repeats many more times, once for each repetition of xs:complexType</p>
  
  <blockquote>
    <p>[xjc] failure in the XJC task. Use the Ant -verbose switch for more details<br>
        compile:  duration 2 seconds<br>
        BUILD FAILED<br>
        C:\Temp\jaxb\apps\create-marshal\build.xml:29: unable to parse the schema. Error messages should have been provided<br>
            at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:520)<br>
            at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:457)<br>
            at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:380)<br>
            at com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:103)<br>
            at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)<br>
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br>
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)<br>
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)<br>
            at java.lang.reflect.Method.invoke(Method.java:606)<br>
            at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)<br>
            at org.apache.tools.ant.Task.perform(Task.java:348)<br>
            at org.apache.tools.ant.Target.execute(Target.java:435)<br>
            at org.apache.tools.ant.Target.performTasks(Target.java:456)<br>
            at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)<br>
            at org.apache.tools.ant.Project.executeTarget(Project.java:1364)<br>
            at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)<br>
            at org.apache.tools.ant.Project.executeTargets(Project.java:1248)<br>
            at org.apache.tools.ant.Main.runBuild(Main.java:851)<br>
            at org.apache.tools.ant.Main.startAnt(Main.java:235)<br>
            at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)<br>
            at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)  </p>
  </blockquote>
  
  <p>Here is the ant build file:  </p>
  
  <pre><code>&lt;?xml version="1.0" standalone="yes"?&gt;
  &lt;!--  Copyright 2004 Sun Microsystems, Inc. All rights reserved. --&gt;
  &lt;project basedir="." default="run"&gt;
  &lt;description&gt;This sample application demonstrates how to use the ObjectFactory class to create a Java content tree from scratch and marshal it to XML data. It also demonstrates how to add content to a JAXB List property.&lt;/description&gt;
  &lt;record name="build.log" loglevel="verbose" action="start"/&gt;
  &lt;property name="jaxb.home" value="../.." /&gt;
  &lt;path id="classpath"&gt;
      &lt;pathelement path="src" /&gt;
      &lt;pathelement path="classes" /&gt;
      &lt;pathelement path="schemas" /&gt;
      &lt;fileset dir="${jaxb.home}" includes="lib/*.jar" /&gt;
  &lt;/path&gt;
  &lt;taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"&gt;
      &lt;classpath refid="classpath" /&gt;
  &lt;/taskdef&gt;
  &lt;!--compile Java source files--&gt;
  &lt;target name="compile" description="Compile all Java source files"&gt;
      &lt;echo message="Compiling the schema..." /&gt;
      &lt;mkdir dir="gen-src" /&gt;
      &lt;mkdir dir="gen-src/primer/po" /&gt;
      &lt;xjc schema="POCD_MT000040_SDTC.xsd" package="primer.po" destdir="gen-src"&gt;
          &lt;produces dir="gen-src/primer/po" includes="**/*.java" /&gt;
      &lt;/xjc&gt;
      &lt;echo message="Compiling the java source files..." /&gt;
      &lt;mkdir dir="classes" /&gt;
      &lt;javac destdir="classes" debug="on"&gt;
        &lt;src path="src" /&gt;
        &lt;src path="gen-src" /&gt;
        &lt;classpath refid="classpath" /&gt;
      &lt;/javac&gt;
    &lt;/target&gt;
    &lt;target name="run" depends="compile" description="Run the sample app"&gt;
      &lt;echo message="Running the sample application..." /&gt;
      &lt;java classname="Main" fork="true"&gt;
        &lt;classpath refid="classpath" /&gt;
      &lt;/java&gt;
    &lt;/target&gt;
    &lt;target name="javadoc" description="Generates javadoc" depends="compile"&gt;
      &lt;echo message="Generating javadoc..." /&gt;
      &lt;mkdir dir="docs/api" /&gt;
      &lt;javadoc sourcepath="gen-src" destdir="docs/api" windowtitle="create-marshal (formerly SampleApp3)" useexternalfile="yes"&gt;
        &lt;fileset dir="." includes="gen-src/**/*.java" excludes="**/impl/**/*.java" /&gt;
      &lt;/javadoc&gt;
    &lt;/target&gt;
    &lt;target name="clean" description="Deletes all the generated artifacts."&gt;
      &lt;delete dir="docs/api" /&gt;
      &lt;delete dir="gen-src" /&gt;
      &lt;delete dir="schemas" /&gt;
      &lt;delete dir="classes" /&gt;
    &lt;/target&gt;
  &lt;/project&gt;
  </code></pre>