如何解决使用具有嵌套导入的架构来验证Java中的XML
我正在尝试使用嵌套导入针对xsd模式验证XML字符串:
types.xsd
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/types" xmlns:gef="http://www.example.com/geofencing">
<xsd:import namespace="http://www.example.com/implementations" schemaLocation="implementationsV2.1.xsd"/>
<xsd:element name="geofencingMode">
<xsd:complexType>
<xsd:attribute name="mode" type="gef:modeId"/>
<xsd:attribute name="period" type="gef:periodId"/>
</xsd:complexType>
</xsd:element>
</schema>
implementationsV2.1.xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema targetNamespace="http://www.example.com/implementations" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.example.com/geofencing" schemaLocation="geofencingServiceV2.1.xsd">
<xsd:annotation>
<xsd:documentation>...</xsd:documentation>
</xsd:annotation>
</xsd:import>
</schema>
geofencingServiceV2.1.xsd
<xsd:schema targetNamespace="http://www.example.com/geofencing"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.com/geofencing">
<xsd:simpleType name="modeId">
...
</xsd:simpleType>
</schema>
具有以下验证代码:
private void validateResponseSchema(String response) throws IOException,SAXException {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new ClassPathResource("types.xsd").getURL());
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new ByteArrayInputStream(response.getBytes())));
}
我得到了错误:
org.xml.sax.SAXParseException; systemId: file:/.../types.xsd; lineNumber: 358; columnNumber: 87; src-resolve.4.2: Error resolving component 'gef:modeId'.
It was detected that 'gef:modeId' is in namespace 'http://www.example.com/geofencing',but components from this namespace are not referenceable from schema document 'file:/.../types.xsd'.
If this is the incorrect namespace,perhaps the prefix of 'gef:modeId' needs to be changed. If this is the correct namespace,then an appropriate 'import' tag should be added to 'file:/.../types.xsd'.
如果我直接在geofencing
中导入types.xsd
命名空间,则可以使用,但是我不明白为什么需要这样做。
是否有一个选项也许我会丢失?该错误似乎特定于引擎盖下使用的Java库(Apache xerces),因为该模式否则有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。