I have several XSDs that reuse the same entities. For example, both the XSDs for the ProductPurchaseRequest.xsd
and ProductQuoteRequest.xsd
both have a <product>
tag in them to describe the product in question. For this reason I created a Product.xsd
file to define the <product>
tag and both ProductPurchaseRequest.xsd
and ProductQuoteRequest.xsd
import the Product.xsd
with a `.
I would like to use Castor to generate Java classes from theses XSDs, and for both of them to use the same class for representing the Product
so that I could reuse the same logic for mapping them to our model's ProductModel
class.
Can Castor do this? If so, what would be the Ant task syntax for it. If not, would perhaps JAXB be a better alternative?
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
Below is an example of how this could be done using JAXB:
Product
Since multiple XML schemas import Product.xsd we can leverage episode files so that the classes corresponding to Product.xsd are only generated once.
ProductPurchaseRequest.xsd
Below is an example of an XML schema that imports Product.xsd:
When we generate classes from this XML schema we will reference the episode file we created when we generated Java classes from Product.xsd.
ProductQuoteRequest.xsd
Below is another example of an XML schema that imports Product.xsd:
Again when we generate classes from this XML schema we will reference the episode file we created when we generated Java classes from Product.xsd.
For More Information
So I was able to get the whole thing working using the JAXB reference implementation. The trick was to realise that the download from JAXB site is a downloader and not the actual libraries! Double-click to accept the licence and the libraries will download locally. Created a test folder (
JAXB_TEST
) and copied all the downloaded.jar
s to alib
subfolder. I put all my XSDs inXSD
subfolder. After that I ran the following Antbuild.xml
file.The only problem I had was that I had an xsd with a root tag called
<product>
that anonymous extended theProduct
type to add aversion
attribute (which I always like to have on my root tag's) which was causing a name conflict for JAXB. So instead, I turned the anonymous type into a named type (i.e.TopLevelProduct
) and set the root to that type and JAXB was happy with that.