Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jaxb; 2 schemas with common xs:import

Posted on 2006-07-08
4
Medium Priority
?
452 Views
Last Modified: 2013-11-19
Hello,
I am using JAXB in my j2ee project.
I have two XML Schemas; Report.xsd and Graph.xsd
Both of these schemas import a common element defined in Query.xsd
I can generate the Java classes to represent these schemas using Jaxb an manipulate DOMs an so on, no problem.
The probelm is; I thought by having both Report and Graph import Query, that when I called:
 graph.getQuery().get( 0 );
or:
 report.getQuery().get( 0 );
that I'd get back an object of type:
 com.mycompany.myproject.query.Query;
but what I actually get back is a:
 com.mycompany.myproject.graph.Query;
or:
 com.mycompany.myproject.report.Query;
depending on which object I called on.
This is frustrating because I expected both Report and Graph to use objects with the same interface so I could process the Query irrespective of whether it was a Report query or a Graph query.
The generated report.Query and graph.Query don't even implement any interface in common.
Could someone advise me how to achieve what I'm trying to do?  Is there a way to tweak JAXB to do this?
Cheers,

0
Comment
Question by:blackfrancis75
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:astorer
ID: 17065966
Are you using namespaces in the XSDs ?  I'd suggest you should be.
What namespace do you use for each XSD and can you show the actual import statements that you use in each?
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 17066526
sorry, I meant that I was including the query schema, not importing it.
Both report.xsd and graph.xsd have the line:
<xs:include schemaLocation="./query.xsd" ></xs:include>
at the top.
Will changing this to an xs:import statement and using a different namespace result in the desired common interface I'm after?  
I don't understand how that would work..
0
 
LVL 5

Author Comment

by:blackfrancis75
ID: 17066739
If I use xs:import instead of xs:include, I get a parse error in my Report.xsd in the section:
  <xs:sequence>
    <xs:element ref="query" maxOccurs="unbounded"/>
  </xs:sequence>

"cannot resolve the name 'query' to a(n) 'element declaration' component"
0
 
LVL 4

Accepted Solution

by:
astorer earned 2000 total points
ID: 17077415
When using multiple schemas, you should use namespaces - and that means eacxh XSD is in its own different namespace.  So suppose there are 3 files  report.xsd, graph.xsd and based.xsd (containing some base definitions).

report.xsd  begins
============
<xs:schema targetNamespace="http://mystuff.com/report"
    xmlns:base="http://mystuff.com/base"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <xs:import namespace="http://mystuff.com/base" schemaLocation="base.xsd" />

graph.xsd begins
===========
<xs:schema targetNamespace="http://mystuff.com/graph"
    xmlns:base="http://mystuff.com/base"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <xs:import namespace="http://mystuff.com/base" schemaLocation="base.xsd" />

base.xsd begins
==========
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="http://mystuff.com/base">


Now when you run the JAXB xjc tool, it generates
 - the types for report.xsd into classes in package com.mystuff.report
 - the types for graph.xsd into classes in  package com.mystuff.graph
 - the types for base.xsd into classes in  package com.mystuff.base

This should prevent the problems you have with the same class appearing in different or wrong packages.

namespaces are irritating things but unfortunately, you must use them if you have multiple schemas importing each other.





   
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question