14

我正在尝试进行架构验证,目前使用的是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 文件中使用。我正在ObjectStatusDateType从另一个 .xsd 文件中导入文件——我已经对其文件路径进行了三次检查。命名空间也被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>

有没有人遇到过这种类型的问题?

4

4 回答 4

12

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

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

<import namespace="http://www.whatever.gov" location="../wherever" />
<import namespace="http://www.whatever.gov" location="../folder/superawesomeschema.xsd" />
于 2013-09-18T16:59:09.143 回答
6

很多人以前都遇到过这类问题。每当您的验证器出于某种原因未加载您希望它加载的架构文档时(并认为它正在加载),它就会出现。

要确认诊断:尝试在 ObjectHistory_1_0.xsd 中引入一个错误——比如说,格式正确的错误——,并查看系统是否会抱怨。

于 2013-09-17T15:57:16.003 回答
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/endorsedjaxp.properties文件<jdk-home>/jre

javax.xml.validation.SchemaFactory\:http\://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory
于 2013-11-29T10:05:49.880 回答
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> 
于 2014-06-25T10:21:53.563 回答