【发布时间】:2013-09-02 19:36:43
【问题描述】:
如何根据 XSD Schema 验证 XML,其中包含没有 schema-location 的导入?
XSD 的片段:
<xs:schema xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types
xmlns:tns="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://schemas.microsoft.com/exchange/services/2006/types"
elementFormDefault="qualified" version="Exchange2010_SP2" id="types">
<xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
...
已经阅读并尝试过:
无法从架构中删除此导入,因为它包含 xml:lang 属性的引用。
在 variant 1 ResourceResolver 中用 systemId = null
触发的 resolveResource 方法public class ResourceResolver implements LSResourceResolver {
public LSInput resolveResource(String type, String namespaceURI,
String publicId, String systemId, String baseURI) {
//Some implementation
return new Input(publicId, systemId, resourceAsStream);
在 variant 2 中尝试这样:
SchemaFactory sFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//sFactory.setResourceResolver(new ResourceResolver());
Schema schema = sFactory.newSchema(new Source[] {
new StreamSource("http://www.w3.org/XML/1998/namespace"),
new StreamSource(MailGateMQBinding.class.getResourceAsStream("/types.xsd")),
});
validator = messageSchema.newValidator();
source = new DOMSource(inDocBody);
validator.validate(source);
但有一个例外:
没有new StreamSource("http://www.w3.org/XML/1998/namespace") org.xml.sax.SAXParseException:src-resolve:无法将名称“xml:lang”解析为(n)“属性声明”。
还有这个new StreamSource("http://www.w3.org/XML/1998/namespace")
org.xml.sax.SAXParseException: s4s-elt-character: 'xs:appinfo' 和 'xs:documentation' 以外的架构元素中不允许使用非空白字符。看到“xml:”命名空间'。
任何帮助将不胜感激。
【问题讨论】:
-
使用 Saxon XSD 处理器,此命名空间的知识是内置的,因此您无需提供位置。我猜你正在使用 Apache Xerces 处理器?
-
@MichaelKay :是的,我的 EE 容器 IBM WebSphere Application Server 使用 Apache Xerces 处理器(也许,由 IBM 修改)。
标签: java xml validation xsd