针对包含xsd:import的XSD验证XML而不使用位置 [英] Validating XML against XSD containing xsd:import without location

查看:274
本文介绍了针对包含xsd:import的XSD验证XML而不使用位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何针对包含没有架构位置的导入的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 =qualifiedversion =Exchange2010_SP2id =types>
< xs:import namespace =http:// www.w3.org/XML/1998/namespace\"/>
...

已经阅读并尝试过:



这一个这也是 ......不成功。



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



变体1 中,ResourceResolver解析了resolveResource方法with systemId = null

  public class ResourceResolver实现LSResourceResolver {

public LSInput resolveResource(String type,String namespaceURI,
String publicId,String systemId,String baseURI){

//某些实现

返回新的输入(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);

但是有一个例外:
没有新的StreamSource(http ://www.w3.org/XML/1998/namespace) org.xml.sax.SAXParseException:src-resolve:无法将名称'xml:lang'解析为a( n)'属性声明'。



并使用此新的StreamSource(http://www.w3.org/XML / 1998 / namespace)
org.xml.sax.SAXParseException:s4s-elt-character:除了'xs:appinfo之外的架构元素中不允许使用非空格字符'和'xs:documentation'..看到'xml:'命名空间'。



任何帮助都将不胜感激。

解决方案

的XML架构http://www.w3.org/XML/1998/namespace 名称空间位于此处:
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 =qualifiedversion = Exchange2010_SP2id =types>

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

这会有效,但请注意W3C不喜欢大量流量到该文件: http://www.w3.org/2001/xml.xsd 。因此,他们人为地延迟了对它的访问。



许多软件都持有这种模式的本地副本。 (这就是未指定架构位置的原因。架构软件通常从其资源加载它。)



您也可以将其复制到您的计算机并指定该URL的URL另一种方法是使用XML目录,如下所示(catalog.xml):

 <?xml version =1.0?> 
< catalog xmlns =urn:oasis:names:tc:entity:xmlns:xml:catalog>
<! -
这会将名称空间URI重定向到本地模式文件
,该文件应该与catalog.xml
- >在同一目录中找到。
< uri name =http://www.w3.org/XML/1998/namespaceuri =xml.xsd/>
< / catalog>

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


How to validate XML against the XSD Schema containing import without schema-location?

Fragment of 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"/>
...

Already read and tried:

This one and this too... Unsuccessfully.

Cannot remove this import from schema, because it contains reference of xml:lang attribute.

In variant 1 ResourceResolver resolveResource method fired with systemId = null

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

In variant 2 tried like this:

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

But have an Exception: without new StreamSource("http://www.w3.org/XML/1998/namespace") org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'xml:lang' to a(n) 'attribute declaration'.

and with this new StreamSource("http://www.w3.org/XML/1998/namespace") org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'.. Saw 'The "xml:" Namespace'.

Any help would be greatly appreciated.

解决方案

The XML schema for http://www.w3.org/XML/1998/namespace namespace is located here: http://www.w3.org/2001/xml.xsd

So, you can just specify its location in <xs:import> in your schema:

<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"/>
...

That will work, but note that W3C doesn't like huge traffic to that file: http://www.w3.org/2001/xml.xsd. So, they delay artificially the access to it.

Many software hold local copies of such schemas. (That's why the schema location is not specified. The schema software typically loads it from its resources).

You may also copy it to you computer and specify the URL to that copy.

An alternative way is to use XML catalog, like this (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>

But you will have to pass somehow that catalog file to your schema processor software (if it supports XML catalogs)

这篇关于针对包含xsd:import的XSD验证XML而不使用位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆