问题
Context: I am generating java classes from xsd files using maven-jaxb plugin. All my .xsd files are in a single location - src/main/resource directory.
Problem: Everything works fine when xsd's don't reference/import other xsd's with a different target namespace. However when the following xsd below (with targetNamespace="http://www.companyA.com/someservice") imports another xsd filename.xsd from a different namespace (namespace="http://www.companyB.com/"), I get the above error: Cannot resolve the name xxx to a(n) 'type definition' component.
Edit: the element name in the current xsd file is 'entityName', and its type is "companyB:entityName" (i.e.) the names are the same.
I then tried invoking xjc on this file from the command line and this generated Java classes correctly. I also made sure that in Eclipse, I am able to ctrl-click/examine source on "type="companyCdm:entityName", which correctly opens the filename.xsd file. However for some reason maven is unable to get to it.
Question: What am I missing? Why is this case (2 namespaces) different from dealing with a single namespace?
Here is my XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.companyA.com/someservice"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:companyB="http://www.companyB.com/version"
targetNamespace="http://www.companyA.com/someservice"
elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:import namespace="http://www.companyB.com/version" schemaLocation="filename.xsd" />
<xsd:element name="MyName" type="MyType" />
<xsd:complexType name="MyType">
<xsd:annotation>
<xsd:documentation>
A list
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="entityName" type="companyB:entityName" maxOccurs="1" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
回答1:
Ok figured out the work-around:
I moved all the dependent xsd's (that the problematic xsd's references) plus a bunch of other un-used xsds to a separate folder and then re-generated code using maven - it works now. It appears that there was some namespace conflict with one of the other un-used xsd's i haven't yet pointed out the specific one.
回答2:
The O'Rielly [Java and XML Chap3][1]
says that using an InputSource will enable you to parse relative paths:
来源:https://stackoverflow.com/questions/9405921/import-namespace-cannot-resolve-the-name-to-an-type-definition-component