James,
When defining the "namespace" attribute, I have done something similar but I
define a unique namespace for each xshema file. You are also missing the
"targetNamespace" attribute in your <xs:schema> line, though I'm not sure if
it is really required. You might try something like this:
CommonTypesSchema.xsd:
----------------------
<xs:schema targetNamespace="
http://www.corsof.com/CommonTypesSchema"
xmlns="
http://www.corsof.com/CommonTypesSchema"
xmlns:xs="
http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
PersonalInfoSchema.xsd:
-----------------------
<xs:schema targetNamespace="
http://www.corsof.com/PersonalInfoSchema"
xmlns="
http://www.corsof.com/PersonalInfoSchema"
xmlns:xs="
http://www.w3.org/2001/XMLSchema"
xmlns:cts="
http://www.corsof.com/CommonTypesSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import
namespace="
http://www.corsof.com/CommonTypesSchema"
schemaLocation="CommonTypesSchema.xsd"/>
This setup has worked for me, though I'm not sure if it is the best way to
do this.
Jerald Pratt
-----Original Message-----
From: James Henderson [mailto:jameshenderson_at_CORSOF.COM]
Sent: Monday, March 17, 2003 8:34 AM
To: JAXB-INTEREST_at_JAVA.SUN.COM
Subject: Re: Generating to different packages throgh inclusion
On Wed, 12 Mar 2003 12:42:36 -0800, Kohsuke Kawaguchi
<Kohsuke.Kawaguchi_at_Sun.COM> wrote:
>Everything with the same target namespace will go into the same Java
>package. And there's no exception to it.
>
>It doesn't matter whether you use two xsd files or one file.
>
>So the short answer of the status quo is that there's no way you can
>achieve what you want. I think it can be considered as a bug of RI for
>failing to raise an error for those two conflicting customizations.
>
>That said, I think I'm seeing this request (ability to sub-divide one
>namespace into multiple Java packages) fairly frequently, and I think
>it's well founded. Some schemas are really big, and naturally one would
>want to split them into sub-packages.
>
>regards,
>--
>Kohsuke KAWAGUCHI 408-276-7063 (x17063)
>Sun Microsystems kohsuke.kawaguchi_at_sun.com
I do not observe this behavior. (However, it is quite possible that I
have made an error.)
Given the following schemas, CommonTypesSchema.xsd and
PersonalInfoSchema.xsd (which imports CommonTypesSchema.xsd via
an <import> tag):
CommonTypesSchema.xsd
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="
http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="addressType">
<xs:sequence>
<xs:element name="line1" type="xs:string"/>
<xs:element name="line2" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="nameType">
<xs:sequence>
<xs:element name="first" type="xs:string"/>
<xs:element name="middle" minOccurs="0"/>
<xs:element name="last" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="contactInfoType">
<xs:sequence>
<xs:element name="phone" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="fax" type="xs:string"/>
<xs:element name="website" type="xs:anyURI"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
PersonalInfoSchema.xsd:
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="
http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import schemaLocation="CommonTypesSchema.xsd"/>
<xs:element name="personalInfo" type="personalInfoType"/>
<xs:complexType name="personalInfoType">
<xs:sequence>
<xs:element name="name" type="nameType"/>
<xs:element name="address" type="addressType"/>
<xs:element name="contactInfo" type="contactInfoType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Note that neither schema specifies a 'targetNamepsace' attribute,
or a default namespace. Also note that the <import> tag does not
specify the 'namespace' attribute.
I have validated both schemas using XMLSpy, but when I use xjc
to compile PersonalInfoSchema.xsd I receive the following
compilation error:
C:\>%JAXB_HOME%/bin/xjc PersonalInfoSchema.xsd -p personalinfo
parsing a schema...
[ERROR] src-import.1.1: The namespace attribute 'null' of an <import>
element information item must not be the same as the targetNamespace of the
schema it exists in.
line 9 of PersonalInfoSchema.xsd
It appears that the 'namespace' attribute of the import tag
cannot be the same as the targetNamespace it exists in. I
thought perhaps, the compiler did not like the fact that the
'namespace' attribute was null, so I revised the <schema>
elements of both schemas as follows:
CommonTypesSchema.xsd:
----------------------
<xs:schema targetNamespace="
http://www.corsof.com/"
xmlns="
http://www.corsof.com/" xmlns:xs="
http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
PersonalInfoSchema.xsd:
-----------------------
<xs:schema targetNamespace="
http://www.corsof.com/"
xmlns="
http://www.corsof.com/" xmlns:xs="
http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
I then validated both schemas again using XMLSpy, and
recompiled only to receive the same error. I then
changed the <import> tag within the PersonalInfoSchema to
specify the namespace of CommonTypesSchema as follows:
<xs:import namespace="
http://www.corsof.com/"
schemaLocation="CommonTypesSchema.xsd"/>
I then validated both schemas again using XMLSpy, and
recompiled only to receive a new error:
[ERROR] src-import.1.1: The namespace attribute '
http://www.corsof.com/' of
an <import> element information item must not be the same as the
targetNamespace of the schema it exists in.
line 9 of PersonalInfoSchema.xsd
However, according to your initial statement, everything
with the same target namespace will go into the same Java
package. What am I doing incorrectly?