Solved

jaxb; 2 schemas with common xs:import

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to dynamically set the form action using jQuery.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now