SAXParseException;src-resolve:无法将名称“...”解析为“类型定义”组件

16

我正在尝试进行架构验证,目前使用的是javax.xml.validation.SchemaFactory。不幸的是,当我调用newSchema(Source schema)函数时,出现以下错误:

Caused by: org.xml.sax.SAXParseException; systemId: file:/C:/Users/C42056/Documents/workspace-sts-3.2.0.RELEASE/cec-sample-ws-integration-2-war/target/classes/WEB-INF/schemas/xsd/individual/PrivateComponentTypes_4_0.xsd; lineNumber: 33; columnNumber: 88; src-resolve: Cannot resolve the name 'utility:ObjectStatusDateType' to a(n) 'type definition' component.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseLocal(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseLocalElements(Unknown Source)
at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
at com.sei.ec.xml.validation.SimpleXmlValidator.loadSchema(SimpleXmlValidator.java:70)
at com.sei.ec.xml.validation.SimpleXmlValidator.<init>(SimpleXmlValidator.java:83)
... 75 more

utility:ObjectStatusDateType元素在我传递给newSchema(Source schema)函数的.xsd文件中使用。我正在从另一个.xsd文件中导入ObjectStatusDateType,对于该文件路径,我已经进行了三重检查。 utility命名空间也已正确声明。

这是我要传递给函数的模式片段(LocateCoverageIndexesByIdentifier_3_0.xsd):

<xs:import namespace="http://www.sei.com/utility/1/" schemaLocation="../../utility/InvocationOutcome_1_0.xsd"/>
<xs:import namespace="http://www.sei.com/utility/1/" schemaLocation="../../utility/ObjectHistory_1_0.xsd"/>
<xs:import namespace="http://www.sei.com/individual/component/4/" schemaLocation="../PrivateComponentTypes_4_0.xsd"/>
<xs:import namespace="http://www.sei.com/individual/shared/5/" schemaLocation="../IndividualTypes_5_0.xsd"/>
.
. <!-- Some more stuff -->
.
<xs:element name="coveragePeriod" 
            type="utility:ObjectStatusDateType" 
            minOccurs="0"/>

并且这是来自 ObjectHistory_1_0.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:tns="http://www.sei.com/utility/1/" 
           targetNamespace="http://www.sei.com/utility/1/" 
           elementFormDefault="qualified" 
           attributeFormDefault="unqualified" 
           version="1.0">
.
. <!-- Some more stuff -->
.
  <xs:complexType name="ObjectStatusDateType">
    <xs:sequence>
      <xs:element name="effectiveDate" type="xs:date"/>
      <xs:element name="cancelDate" type="xs:date" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

最后,豆子

<bean id="locateClaimValidator" 
      class="com.sei.ec.xml.validation.SimpleXmlValidator">
  <constructor-arg>
    <value>classpath:WEB-INF/schemas/xsd/individual/ci/LocateCoverageIndexesByIdentifier_3_0.xsd
    </value>
  </constructor-arg>
</bean>

有人之前遇到过这种问题吗?


我也遇到了同样的问题。你解决了吗?如果是这样,那么你是怎么解决的? - BATMAN_2008
4个回答

12

我以前遇到过这个问题。在Eclipse中,一切都是有效的,但在运行时出现了错误。您的任何模式是否将多个模式导入同一命名空间?

像这样的东西不起作用,但会被Eclipse验证:

<import namespace="http://www.whatever.gov" location="../wherever" />
<import namespace="http://www.whatever.gov" location="../folder/superawesomeschema.xsd" />

这解决了问题。抱歉,我一开始应该在我的第一个代码标签中包含更多的文件(已经进行了编辑)。我正在包括两个xsd文件在http://www.sei.com/utility/1/命名空间下,其中一个是ObjectHistory_1_0.xsd。总是一些愚蠢的小错误... - Mac
抱歉,当另一个命名空间被包含时,无法进行验证。 - ulab
将属性“location”替换为“schemaLocation”(请参见https://www.w3schools.com/xml/el_import.asp) - Mike Argyriou

6
很多人遇到过这种问题。无论何种原因导致您的验证器没有加载您想要加载的模式文档(并且认为它正在加载),都会出现此问题。
为了确认诊断结果:请尝试在ObjectHistory_1_0.xsd中引入一个错误,比如格式不正确,看看系统是否会报错。

你是对的。如果我改变ObjectHistory_1_0.xsd的某些内容,工厂就无法检测到问题。 - Mac

5
使用Xerces,可以通过将特性http://apache.org/xml/features/honour-all-schemaLocations设置为true来解决此问题。
特性http://apache.org/xml/features/honour-all-schemaLocations仅适用于Xerces 2.7.0及以上版本。当前的Java 5.0和6.0版本内置了Xerces 2.6.2。因此,需要使用更新的Xerces库才能使其正常工作,即将xml-apis.jarxercesImpl.jar复制到<jdk-home>/jre/lib/endorsed,并在<jdk-home>/jre中创建一个包含以下行的jaxp.properties文件。
javax.xml.validation.SchemaFactory\:http\://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory

4

当我执行maven插件jaxb2-maven-plugin时,也遇到了同样的问题。 在明确指定要解析的xsd文件后,它就完美地工作了:

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jaxb2-maven-plugin</artifactId>
 <version>1.6</version>
    <executions>
     <execution>
      <id>xjc</id>
      <goals>
       <goal>xjc</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <schemaFiles>MySchema.xsd</schemaFiles>
     <outputDirectory>src/main/java</outputDirectory>
    </configuration>
</plugin> 

这也有助于我 - 明确指向一个 xsd 文件(同一目录中有几个 xsd 文件)。 - jsosnowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接