[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

jaxb; 2 schemas with common xs:import

Posted on 2006-07-08
4
Medium Priority
?
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
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

649 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