8

如何针对包含没有模式位置的导入的 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"/>
...

已经阅读并尝试过:

这个这个也是……不成功。

无法从架构中删除此导入,因为它包含 xml:lang 属性的引用。

变体 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);

变体 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:”命名空间'。

任何帮助将不胜感激。

4

2 回答 2

11

命名空间的 XML 模式http://www.w3.org/XML/1998/namespace位于此处: https ://www.w3.org/2009/01/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="https://www.w3.org/2009/01/xml.xsd"/>
...

这会起作用,但请注意,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>

但是您必须以某种方式将该目录文件传递给您的模式处理器软件(如果它支持 XML 目录)

于 2013-08-30T07:22:58.073 回答
-2

只需删除:

<!DOCTYPE xs:schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd">

从 xml.xsd

并改变

<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/>

如果它是相同的错误,它可能是有用的。

于 2015-11-19T13:40:10.410 回答