我正在努力解决一个问题,这个问题可能非常简单,但却无法推理和分析。我正在使用三种不同的XSD,然后使用XJC创建用于创建Web服务的java类。
Prvilege.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://xmlns.hishekha.org/type"
targetNamespace="http://xmlns.hishekha.org/type"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Privilege" type="Privilege" />
<xs:complexType name="Privilege">
<xs:sequence>
<xs:element name="Id" type="xs:int" />
<xs:element name="Name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Role.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://xmlns.hishekha.org/type"
targetNamespace="http://xmlns.hishekha.org/type"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:privilege="http://xmlns.hishekha.org/type"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import schemaLocation="Privilege.xsd" namespace="http://xmlns.hishekha.org/type" />
<xs:element name="Role" type="Role" />
<xs:complexType name="Role">
<xs:sequence>
<xs:element name="Id" type="xs:int" />
<xs:element name="Name" type="xs:string" />
<xs:element name="Privilege" type="privilege:Privilege" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
User.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:u="http://xmlns.hishekha.org/type"
targetNamespace="http://xmlns.hishekha.org/type"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:role="http://xmlns.hishekha.org/type"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:include schemaLocation="Role.xsd" />
<xs:element name="User" type="u:User" />
<xs:complexType name="User">
<xs:sequence>
<xs:element name="Id" type="xs:int" />
<xs:element name="Username" type="xs:string" />
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />
<xs:element name="Email" type="xs:string" />
<xs:element name="Birthdate" type="xs:date" />
<xs:element name="Role" type="role:Role" />
<xs:element name="IsAccountExpired" type="xs:boolean" />
<xs:element name="IsAccountLocked" type="xs:boolean" />
<xs:element name="IsAccountASLExpired" type="xs:boolean" />
<xs:element name="IsAccountEnabled" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
</xs:schema>
到目前为止,如果我在pom.xml中使用以下maven插件comf调用mvn编译,它可以很好地生成类。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<clearOutputDir>false</clearOutputDir>
<outputDirectory>src/main/java</outputDirectory>
<schemaDirectory>src/main/webapp/schemas</schemaDirectory>
<includes>**/*xsd</includes>
<enableIntrospection>false</enableIntrospection>
</configuration>
</plugin>
但是当我试图在其他具有不同名称空间的xsd中使用它时。
UserServiceOperations.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://xmlns.hishekha.org/services"
targetNamespace="http://xmlns.hishekha.org/services"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:u="http://xmlns.hishekha.org/type"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:import namespace="http://xmlns.hishekha.org/type" schemaLocation="User.xsd" />
<xs:element name="UserResponseOutput">
<xs:complexType>
<xs:sequence>
<xs:element name="User" type="u:User" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
(我正在为Web服务输入输出模式做)它开始抛出以下错误
[INFO] Generating source...
[INFO] parsing a schema...
[ERROR] file:/C:/Users/hishekha.ORADEV/WorkspaceJ2EE/SimpleServices/src/main/web
app/schemas/UserServiceOperations.xsd[14,45]
org.xml.sax.SAXParseException; systemId: file:/C:/Users/hishekha.ORADEV/Workspac
eJ2EE/SimpleServices/src/main/webapp/schemas/UserServiceOperations.xsd; lineNumb
er: 14; columnNumber: 45; src-resolve: Cannot resolve the name 'u:User' to a(n)
'type definition' component.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAX
ParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Err
orHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(
XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.repo
rtSchemaErr(XSDHandler.java:4162)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.repo
rtSchemaError(XSDHandler.java:4145)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getG
lobalDecl(XSDHandler.java:1741)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTrave
rser.traverseNamedElement(XSDElementTraverser.java:405)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTrave
rser.traverseLocal(XSDElementTraverser.java:194)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.trav
erseLocalElements(XSDHandler.java:3618)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.pars
eSchema(XSDHandler.java:633)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema
(XMLSchemaLoader.java:616)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGramma
r(XMLSchemaLoader.java:574)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGramma
r(XMLSchemaLoader.java:540)
at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.n
ewSchema(XMLSchemaFactory.java:252)
at com.sun.tools.xjc.reader.xmlschema.parser.SchemaConstraintChecker.che
ck(SchemaConstraintChecker.java:101) ....
我想我做了一些简单的错误,但无法弄明白。请帮帮我们 注意:所有xsd文件都在同一目录中。
答案 0 :(得分:0)
问题不在UserServiceOperations.xsd
。它正确导入User.xsd
模式,这对于验证对合格User
元素的引用是必要的。
问题出在Role.xsd
。它与Privilege.xsd
属于相同的命名空间,因此您不应该导入,而包括它(如你在User.xsd
做过,包括Role.xsd
)。
不
<xs:import schemaLocation="Privilege.xsd" namespace="http://xmlns.hishekha.org/type" />
但
<xs:include schemaLocation="Privilege.xsd" />
如果您更改了这一行,则可以验证UserServiceOperations.xsd
并使用xjc
生成类。