The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » Metro and JAXB

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

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 29 - Last Post: Nov 17, 2009 10:11 AM by: gopmani Threads: [ Previous | Next ]
nkeaccept

Posts: 9
JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 6, 2005 3:00 AM
  Click to reply to this thread Reply

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

kohsuke

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 6, 2005 9:21 AM   in response to: nkeaccept
  Click to reply to this thread Reply

When you include the same schema document from many schemas by means of <xs:include>, 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 <xs:import> it, not <xs:include> it)

nkeaccept

Posts: 9
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 7, 2005 4:00 AM   in response to: kohsuke
  Click to reply to this thread Reply

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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 7, 2005 7:34 AM   in response to: nkeaccept
  Click to reply to this thread Reply

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

Posts: 9
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 7, 2005 12:12 PM   in response to: kohsuke
  Click to reply to this thread Reply

Hi,

it is the point :-) ,

thank you very mach

Norbert

kohsuke

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 9, 2005 2:58 PM   in response to: nkeaccept
  Click to reply to this thread Reply

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

Posts: 9
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 9, 2005 10:47 PM   in response to: kohsuke
  Click to reply to this thread Reply

Hi,

now I know what I've to do.

Thank you very, very much

Norbert

malachid

Posts: 15
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: May 10, 2005 8:38 AM   in response to: nkeaccept
  Click to reply to this thread Reply

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):
	<goal name="xsd">
		<ant: property environment="env"/>
		
		<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
			<classpath>
				<fileset dir="${env.JAXB_HOME}/lib" includes="*.jar"/>
			</classpath>
		</taskdef>
		
		<xjc target="${basedir}/src/java">
			<schema dir="${basedir}/src/conf" includes="*.xsd"/>
			<binding dir="${basedir}/src/conf" includes="*.xjb"/>
		</xjc>
	</goal>


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

Posts: 2
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jul 23, 2005 7:13 AM   in response to: malachid
  Click to reply to this thread Reply

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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jul 23, 2005 11:12 AM   in response to: ankesh
  Click to reply to this thread Reply

> 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

Posts: 2
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jul 24, 2005 10:52 PM   in response to: kohsuke
  Click to reply to this thread Reply

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.

gopmani

Posts: 1
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Nov 17, 2009 10:11 AM   in response to: nkeaccept
  Click to reply to this thread Reply

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

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 6, 2005 6:14 AM   in response to: nkeaccept
  Click to reply to this thread Reply

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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 7, 2005 10:11 AM   in response to: bruni
  Click to reply to this thread Reply

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

bruni

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 8, 2005 1:32 AM   in response to: kohsuke
  Click to reply to this thread Reply

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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 8, 2005 9:24 AM   in response to: bruni
  Click to reply to this thread Reply

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 <xs:include>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

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 9, 2005 6:39 AM   in response to: kohsuke
  Click to reply to this thread Reply

Thanks for the topic.

I have a problem with the suffix of element names in imported schema. When I use : <jxb:elementName suffix="Element"/>, 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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 9, 2005 11:58 AM   in response to: phuonglh
  Click to reply to this thread Reply

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

I don't have a reference handy, but IIRC, <jaxb:elementName> is a part of <jaxb:schemaBindings>.
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 <jaxb:schemaBindings> element twice.

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

phuonglh

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 10, 2005 2:29 AM   in response to: kohsuke
  Click to reply to this thread Reply

Thank you very much kohsuke!

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

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

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

Failed to parse a schema.


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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 10, 2005 1:23 PM   in response to: phuonglh
  Click to reply to this thread Reply

You need one <schemaBindings> to go to one namespace, and the other <schemaBindings> to go to the other namespace. Use <jaxb:bindings schemaLocation="..."/>

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

phuonglh

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 11, 2005 6:20 AM   in response to: kohsuke
  Click to reply to this thread Reply

Ah, I've done it with your help!

Thank you very much kohsuke.

Have a nice weekend!

bruni

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jun 10, 2005 12:22 AM   in response to: kohsuke
  Click to reply to this thread Reply

Thank you, kohsuke.

I'll try it later as you suggested.

lkamal

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jul 26, 2005 2:53 AM   in response to: bruni
  Click to reply to this thread Reply

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).

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Message">
<xs:complexType>
<xs:sequence>
<xs:element ref="DestinationRouteCode" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="Id" use="optional">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="DestinationRouteCode">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>

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.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:import schemaLocation="../Types/Message.xsd" />
<xs:element name="Request">
<xs:complexType>
<xs:sequence>
<xs:element ref="Message"/>
<xs:element ref="PrealertRequestLevel"/>
</xs:sequence>
<xs:attribute name="Version" type="xs:decimal" use="required" fixed="4.0"/>
</xs:complexType>
</xs:element>
</xs:schema>

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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jul 26, 2005 8:33 AM   in response to: lkamal
  Click to reply to this thread Reply

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

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jul 27, 2005 2:51 AM   in response to: kohsuke
  Click to reply to this thread Reply

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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Jul 27, 2005 10:52 AM   in response to: lkamal
  Click to reply to this thread Reply

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 <xs:import> and <xs:include> 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

Posts: 3
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Aug 30, 2005 1:57 AM   in response to: kohsuke
  Click to reply to this thread Reply

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.

jschweickart

Posts: 2
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Dec 13, 2006 11:15 AM   in response to: kohsuke
  Click to reply to this thread Reply

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

Posts: 2
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Dec 13, 2006 12:44 PM   in response to: jschweickart
  Click to reply to this thread Reply

I discovered my problem. When I restricted the ant <include... > 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

Posts: 3,962
Re: JAXB and xsd:include (common type libraries across multiple schemas)
Posted: Dec 13, 2006 3:11 PM   in response to: jschweickart
  Click to reply to this thread Reply

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.




 XML java.net RSS