我正在尝试进行架构验证,目前正在使用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
函数的.xsd文件中使用newSchema(Source schema)
元素。我从另一个.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>
以前是否有人遇到过此类问题?
答案 0 :(得分:10)
我以前遇到过这个问题。所有在Eclipse中验证的东西,但在运行时破坏了。您的任何模式是否将多个模式导入同一名称空间?
这样的东西不起作用,但将由Eclipse验证:
<import namespace="http://www.whatever.gov" location="../wherever" />
<import namespace="http://www.whatever.gov" location="../folder/superawesomeschema.xsd" />
答案 1 :(得分:6)
之前很多人都遇到过这类问题。无论何种原因,无论何时您的验证器都没有加载您希望它加载的架构文档(并认为它正在加载),它就会出现。
要确认诊断:尝试在ObjectHistory_1_0.xsd中引入错误(例如,格式错误),并查看系统是否抱怨。
答案 2 :(得分:4)
使用Xerces可以通过设置功能来解决
http://apache.org/xml/features/honour-all-schemaLocations
为真。
功能http://apache.org/xml/features/honour-all-schemaLocations
仅可用
来自Xerces 2.7.0。 Java 5.0和6.0的当前版本具有Xerces 2.6.2
内置。因此,必须使用较新的Xerces库才能使其工作,
即。将xml-apis.jar
和xercesImpl.jar
复制到<jdk-home>/jre/lib/endorsed
和
在jaxp.properties
中创建包含
<jdk-home>/jre
文件
javax.xml.validation.SchemaFactory\:http\://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory
答案 3 :(得分: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>