Cad*_*usX 6 java xml validation xsd
如何针对包含没有架构位置的导入的XSD架构验证XML?
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"/>
...
Run Code Online (Sandbox Code Playgroud)
已经阅读并尝试过:
无法从架构中删除此导入,因为它包含xml:lang属性的引用.
在变量1中使用systemId = null触发的ResourceResolver 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);
Run Code Online (Sandbox Code Playgroud)
在变体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);
Run Code Online (Sandbox Code Playgroud)
但是有一个异常:没有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'之外的架构元素中不允许使用非空格字符.Saw'"xml:"命名空间".
任何帮助将不胜感激.
http://www.w3.org/XML/1998/namespace命名空间的XML模式位于:http:
//www.w3.org/2001/xml.xsd
因此,您只需<xs:import>在架构中指定其位置:
<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"
schemaLocation="http://www.w3.org/2001/xml.xsd"/>
...
Run Code Online (Sandbox Code Playgroud)
这将有效,但请注意,W3C不喜欢该文件的大量流量:http://www.w3.org/2001/xml.xsd.因此,他们人为地延迟了对它的访问.
许多软件都拥有此类模式的本地副本.(这就是未指定架构位置的原因.架构软件通常从其资源加载它).
您也可以将其复制到您的计算机并指定该副本的URL.
另一种方法是使用XML目录,如下所示(catalog.xml):
<?xml version="1.0"?>
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<!--
This will redirect the namespace URI to the local schema file,
which should be found in the same directory as the catalog.xml
-->
<uri name="http://www.w3.org/XML/1998/namespace" uri="xml.xsd"/>
</catalog>
Run Code Online (Sandbox Code Playgroud)
但是你必须以某种方式将目录文件传递给模式处理器软件(如果它支持XML目录)