Solved

jaxb; 2 schemas with common xs:import

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 74
difference of if loops 23 49
varialbe initialization 11 33
Randomize in Owl Carousel v1.3.2 6 45
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to dynamically set the form action using jQuery.
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…

809 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