2

I have a bunch of xsd schemas some of which import others. So, whenever I invoke xjc (either the Ant task or the command line tool) on some xsd files which import others, I pass along a bindings file to ensure that the imported classes are not generated again (since they have already been generated for the "upstread" schema). This solution is described in this SO post (answer by Blaise Doughan).

While this approach does work for xsd:complexType types, it does not seem to work for xsd:simpleType types. Here's a narrowed down example to demonstrate this. Say we have two dead-simple schemas one importing the other:

a.xsd   ---import-->  b.xsd

Files are:

a.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://example.a" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:import namespace="http://example.b"
              schemaLocation="b.xsd"/>

</xs:schema>

b.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://example.b" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:b="http://example.b" 
           elementFormDefault="qualified">

   <xs:simpleType name="ResourceType">
      <xs:restriction base="xs:string">
         <xs:enumeration value="alpha"/>
         <xs:enumeration value="beta"/>
      </xs:restriction>
   </xs:simpleType>

   <xs:complexType name="ResourceType2">
        <xs:sequence>
            <xs:element name="a" type="xs:integer"/>
        </xs:sequence>
   </xs:complexType>
</xs:schema>

So basically a.xsd does nothing and only imports b.xsd. when one invokes xjc on a.xsd as follows:

xjc -d output -p example.a -npa a.xsd 

you get:

output/
└── example
    └── a
        ├── ObjectFactory.java
        ├── ResourceType2.java
        └── ResourceType.java

So files were generated for the imported schema. Since we don't want that, we define the following bindings file to pass along:

imported-bindings.xml

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="b.xsd">
        <jxb:bindings node="//xs:simpleType[@name='ResourceType']">
            <jxb:class ref="example.b.ResourceType"/>
        </jxb:bindings>
        <jxb:bindings node="//xs:complexType[@name='ResourceType2']">
            <jxb:class ref="example.b.ResourceType2"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

HOWEVER when we invoke again xjc on a.xsd with the -b option as below:

xjc -d output -p a.example a.xsd -b imported-bindings.xml 

we see the following in the output directory:

output/
└── example
    └── a
        ├── ObjectFactory.java
        └── ResourceType.java

That is, generation of the xsd:complexType "ResourceType2" was indeed suppressed, but xjc went ahead and generated code for the imported xsd:simpleType "ResourceType". Any ideas?

UPDATE I still haven't gotten this to work except by a workaround using automatically generated episode files

Community
  • 1
  • 1
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331

0 Answers0