Solved

jaxb; 2 schemas with common xs:import

Posted on 2006-07-08
4
437 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

726 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