Skip to main content

JAXB and xsd:include (common type libraries across multiple schemas)

29 replies [Last post]
nkeaccept
Offline
Joined: 2004-02-03

Hello,

I have a question about using common type libraries across multiple schemas with JAXB.
The type library is itself a schema without any xsd:element's, only xsd:complexType's and xsd:simpleType's.
The type library is included in several schemas using xsd:include.

Is there any way to get the my type library schema to be generated to a seperate package for all "sub"-schemas to use.
I want to achieve reuse of the types in the type library over several schemas but not generate them into seperate packages.

How can this to be done?

Thanks in advance,

Norbert

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jschweickart
Offline
Joined: 2006-12-13

I think my issue falls into this thread. I have a "master" xsd that xsd:includes several "child" xsds. Each child xsd xsd:includes the same common.xsd. I'm getting xxx already defined errors when running xjc on my master. What is the best strategy to handle this?

jschweickart
Offline
Joined: 2006-12-13

I discovered my problem. When I restricted the ant to just the "master" level schemas, it worked. When I had used *.xsd, it blew up on the "child" xsds, which were already defined by the "master" xsds.

kohsuke
Offline
Joined: 2003-06-09

Hmm. Normally, such precaution shouldn't be necessary, unless you use a schema technique called "chameleon schema".

Most often you can just submit **/*.xsd to XJC and it does the right thing.

bruni
Offline
Joined: 2003-12-23

Thank you, kohsuke.

I'll try it later as you suggested.

lkamal
Offline
Joined: 2005-07-14

Hi All,

I went through this thread to identify how to use a common schema. I'm having a common Message tag in a schema. I need to use it in two schemas. (All are using the same name spaces).

























So first I generated the java classes for the Message schema shown above using; xjc -d C:\myproject -p com.my.xml.message Message.xsd

Then I tried to generate the java classes for the request.xsd which import the Message schema as follows.












When I tried to use xjc for this;

xjc -d C:\myproject -p com.my.xml.request Request.xsd

I get an error as,

parsing a schema...
[ERROR] src-resolve: Cannot resolve the name 'Message' to a(n) 'element declarat
ion' component.
line 9 of Request.xsd

Failed to parse a schema.

As I feel, I have to specify the place from where to find the generated classes for Message type which are used in the Request.xsd. But is there a way to do that?

Am I doing something wrong? Any ideas please.

Thanks in advance.

kohsuke
Offline
Joined: 2003-06-09

If all your schemas are using the same namespace. You shouldn't be importing it. Instead, you should be including it.

I noticed that you are probably trying to put classes from one namespace into more than one package. I don't think the current set of customization allow you to do this.

So probably the only option for you is to use XJC to generate code, then take the ownership and perhaps use IDE or something to move the classes around.

lkamal
Offline
Joined: 2005-07-14

My basic requirement is to reuse a set of elements.

So could you tell me the way to achieve the reuse of the elements? As I'm reusing the types in the schema, I need to reuse the objects generated for those types.

The Message class generated from the message type has to be shared by all the other types like Request class, Response class.

Basically what I'm trying to do is to reuse the same set of classes inside many other classes (generated for different schemas).

Thanks in Advance for your comments.

kohsuke
Offline
Joined: 2003-06-09

It's quite OK to split the definitions into multiple schema documents. So with that respect you did the right thing.

The part that I believe you didn't get right is that and has specific meanings and usually they are not interchangeable. That's a schema correctness issue and not really a JAXB issue.

What you are describing further (you got schema S2 that refers to S1. You want to generate Java code J1 from some schema S1, and then later generate Java code J2 from schema S2 and you want J2 to be using J1.) is what we call "separate compilation."

The story of separate compilation in JAXB is still weak. Today what we are suggesting people to do is to have S2 generate both J1 and J2, and remove the duplicate pieces.

Even if we improve the separate compilation story, it will probably start with namespace as the smallest unit.

lkamal
Offline
Joined: 2005-07-14

Yes Kohsuke, That's exactly what I meant.

So when JAXB proceeds further if you could think on this as well, it'll be more helpful as it help the code reuse.

gopmani
Offline
Joined: 2009-11-17

Hi nkeaccept,

I have a similar requirement, so could you please let me know about the way to achieve the compilation of Sub-schemas and the Common library java files need to be in some other package

Thanks & Regards,
gopi

bruni
Offline
Joined: 2003-12-23

Hi there,

I have a problem with overture aws xsd files. There are two common xsd markets.xsd and types.xsd which are included by dtc_request.xsd and dtc_response. when I compile them, the types declared in markets.xsd and types.xsd cannot be generated with xjc.

Could you help me?

Thanks in advance.

kohsuke
Offline
Joined: 2003-06-09

That's the exact same problem discussed in this thread. What aspect of it would you like to know more?

bruni
Offline
Joined: 2003-12-23

Thank you for your reply!

At the begginning, I want to find a solution which doens't need to modify the original xsd schemas, because overture provides those schemas. After I read this thread, I tried to modify two xsd schemas by replacing 'xs:include' with 'xs:import', but I failed again. It said the imported xsd has the same namespace. Does it mean if I want to import some xsd, the namespace must be different? Could you give me some details about replacing xs:include with xs:import?

And at the end, I have to change to xmlbeans, there was no problem. But I hope to use sun's jaxb some day.

Message was edited by: bruni

kohsuke
Offline
Joined: 2003-06-09

I looked at
https://x-secure.overture.com/schema/dtc/1.2/dtc_request.xsd
and
https://x-secure.overture.com/schema/dtc/1.2/dtc_response.xsd

And I noticed that these two schemas use the same naemspace URI. This is different from the original problem discussed in this topic, which involves in having two schemas with different namespace URIs, which include the same schema document.

Generally, defining one namespace in multiple documents without referencing each other is going to cause interoperability issues. For example I know for sure that Xerces won't handle this correctly.

If you've got errors from JAXB saying "duplicate definitions", that's technically a bug in the JAXB RI. I'm thinking about fixing this, but Overture would probably want to change their schema anyway.

For now, to make those schemas compile, you can remove s from either one of the documents, and then
compile them as:

$ xjc dtc_*.xsd

If this doesn't work, try the -nv option. And if you still have problems, please also post the error messages.

phuonglh
Offline
Joined: 2004-09-17

Thanks for the topic.

I have a problem with the suffix of element names in imported schema. When I use : , only elements of parent XSD are generated with this suffix, but not elements of child schema (imported schema).

Is there any way to get all elements of imported schema to have the same suffix as those of importing shema ?

Thank you for your help!

kohsuke
Offline
Joined: 2003-06-09

I suspect that the issue you are experiencing is different from this topic.

I don't have a reference handy, but IIRC, is a part of .
If so, it only takes effect per namespace.

The fact that you are importing a schema suggests that
you use at least 2 namespaces, and if so you need to
declare the element twice.

I'm thinking about writing a simple customization file
editor. I think that would forestall confustions like
this and improve productivity...

phuonglh
Offline
Joined: 2004-09-17

Thank you very much kohsuke!

Yes, I've used 2 namespaces. But if I declare the element twice, xjc generates following error :

[b]parsing a schema...
[ERROR] Multiple are defined for the target namespace "http://www.loria.fr/led/tagml2/jaxb"
line 7 of bindings.xml

[ERROR] Another is defined here
line 17 of bindings.xml

Failed to parse a schema.
[/b]

Thank you for your help.

PS. How can I upload my schema and binding files to forum? I can't find a way to do this.

kohsuke
Offline
Joined: 2003-06-09

You need one to go to one namespace, and the other to go to the other namespace. Use

You can't upload a file to a forum, but you can send them to us!

phuonglh
Offline
Joined: 2004-09-17

Ah, I've done it with your help!

Thank you very much kohsuke.

Have a nice weekend!

kohsuke
Offline
Joined: 2003-06-09

When you include the same schema document from many schemas by means of , you are creating multiple schema definitions similar to each other but nevertheless differ in their target namespaces.

Because of that difference, JAXB will not be able to take the advantage of their similarities.

Many schemas written in that way can be actually re-written so that you don't define multiple copies, so I suggest you look into that approach
(IOW, think of a way to it, not it)

nkeaccept
Offline
Joined: 2004-02-03

Hi,

thank you very much for your explanation.

By the way, I've following problem when I compile for example the two schemas "purchaseRequest.xsd" and "purchaseResponse.xsd" into same package "de.mycompany.eft" (with xjc in build.xml). No any error was logged at this time, but at the runtime subdirectory it seems to be only the last one (purchaseRespone overwrite the purchaseRequest stuff) valid for Marshaling/Unmarshaling?
Am I doing basically something not correct?

Norbert

kohsuke
Offline
Joined: 2003-06-09

If you run XJC twice to compile them, XJC won't notice that it's overwriting the files, so you may end up getting incorrect set of files at the end.

If you run XJC once and compiled two files at once, then XJC should recognize that and report an error.

nkeaccept
Offline
Joined: 2004-02-03

Hi,

it is the point :-) ,

thank you very mach

Norbert

kohsuke
Offline
Joined: 2003-06-09

OK, I saw your schema.

The problem is that you are using The -package option of XJC (in the build script), and this causes all the schema components to be generated into this package.

Since your common type library is used in two namespaces, XJC tries to generate two copies of the similar (but different) classes. The -package option tells XJC to generate both into the same package, and hence the crash.

I agree that the error message is pretty cryptic,
as it says "FooBar class in line 123 of abc.xsd is colliding with FooBar class in line 123 of abc.xsd", but given the way the schemas are organized, this is somewhat unavoidable.

Given that in the common type library you are mostly defining types, not elements, it seems to me that you can probably redesign it so that you can import this type library, not include it.

nkeaccept
Offline
Joined: 2004-02-03

Hi,

now I know what I've to do.

Thank you very, very much

Norbert

malachid
Offline
Joined: 2004-05-16

As I only recently managed to get this all working, here are some notes....

1. only do the globalBindings in the common.xsd
2. define the package name in each xsd, NOT in xjc
3. use xsd:import in all but common.xsd
4. make sure to set xmlns:common in the namespace declarations of all but common.xsd (assuming that common just does xmlns=)
5. compile all the xsds at the same time. the method I use for this (maven) is (editor: added space between 'ant:' and 'property' so it would quit showing it as a smiley face):
[code]










[/code]

You will notice there has always been a lot of questions on how to get this working on the forums. Usually, this turned out to be because we were compiling one xsd at a time or using xsd:include

Malachi

ankesh
Offline
Joined: 2005-07-22

My doubts are –

Whenever I use xsd:import tag in the xsd importing the common.xsd, I need to provide the namespace attribute. Otherwise, the element in the Common xsd is not recognized.
If I provide a namespace attribute some value, I need to have same value set in the targetnamespace of the Common (Which is not recommended above).
If I provide namespace and Common’s targetnamespace same value, I am able to generate the java source files, but, while unmarshallinng a XML, the uri for the common xsd does not match with the XML resulting in unmarshalling exception.

Could you please provide me a resolution, if you have solved this.

kohsuke
Offline
Joined: 2003-06-09

> Whenever I use xsd:import tag in the xsd importing the common.xsd, I need to provide the namespace attribute.

No, you don't have to. What do you mean by
"Otherwise, the element in the Common xsd is not recognized"?

I think we need to see more details. Please feel free
to file it as an issue on http://jaxb.dev.java.net/

ankesh
Offline
Joined: 2005-07-22

I am sending you my xsds on your mail ID. Whenever I run xjc on the same, I get this error -
[xjc] Compiling file:/D:/xsd/AccountInquir
yRequest.xsd
[xjc] [ERROR] undefined element declaration 'TXN_INFO'
[xjc] line 9 of AccountInquiryRequest.xsd

Please suggest some resolution to that.