8

schema-location なしでインポートを含む 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では、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);

バリアント 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: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'.. Saw 'The "xml:" Namespace '。

どんな助けでも大歓迎です。

4

2 に答える 2

11

名前空間の XML スキーマは、httpshttp://www.w3.org/XML/1998/namespace ://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 に答える