简体   繁体   English

使用 Java 中的 Xerces 针对 XSD 1.1 进行 XML 验证

[英]XML validation against XSD 1.1 with Xerces in Java

I have installed Xerces through Maven:我已经通过 Maven 安装了 Xerces:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.11.0</version>
    </dependency>            
</dependencies>

I then tried the code given in this example from the Xerces FAQ to validate a XML file against a schema in version 1.1.然后,我尝试了Xerces FAQ中此示例中给出的代码,以根据 1.1 版中的模式验证 XML 文件。 This is my code:这是我的代码:

private static void validateFile(File xmlFile, File xsdFile) throws SAXException, IOException
{
    // 1. Lookup a factory for the W3C XML Schema language
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");

    // 2. Compile the schema.
    File schemaLocation = xsdFile;
    Schema schema = factory.newSchema(schemaLocation);

    // 3. Get a validator from the schema.
    Validator validator = schema.newValidator();

    // 4. Parse the document you want to check.
    Source source = new StreamSource(xmlFile);

    // 5. Check the document
    try
    {
        validator.validate(source);
        System.out.println(xmlFile.getName() + " is valid.");
    }
    catch (SAXException ex)
    {
        System.out.println(xmlFile.getName() + " is not valid because ");
        System.out.println(ex.getMessage());
    }
}

The code only yields this exception:该代码仅产生此异常:

java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://www.w3.org/XML/XMLSchema/v1.1 could be loaded
at javax.xml.validation.SchemaFactory.newInstance(SchemaFactory.java:204)
at example.xml.XSDValidator.validateFile(XSDValidator.java:65)

Seems like I failed to configure/install Xerces correctly.似乎我未能正确配置/安装 Xerces。 Please help me get this working, the XML files force me to use the schema in 1.1, I got a normal validator for 1.0 running but I have huge problems with this.请帮我完成这项工作,XML 文件强制我使用 1.1 中的模式,我得到了一个正常运行的 1.0 验证器,但我对此有很大的问题。 I appreciate every hint!我感谢每一个提示!

It looks that you need Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta) version, which isn't in maven repository.看起来您需要 Xerces2 Java 2.11.0 (XML Schema 1.1) (Beta) 版本,它不在 maven 存储库中。 You can download it from Xerces website, and install it to your local maven repository: mvn install:install-file -Dfile=xercesImpl.jar -DgroupId=xerces -DartifactId=xercesImpl -Dversion=2.11.0.beta -Dpackaging=jar Then you will be able to include it in your Maven project dependencies:您可以从Xerces网站下载它,并将其安装到本地 maven 存储库: mvn install:install-file -Dfile=xercesImpl.jar -DgroupId=xerces -DartifactId=xercesImpl -Dversion=2.11.0.beta -Dpackaging=jar然后您将能够将其包含在您的 Maven 项目依赖项中:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0.beta</version>
</dependency>   

I will add another answer, because for me this dependency did not work (same error as described by OP):我将添加另一个答案,因为对我来说这种依赖不起作用(与 OP 描述的错误相同):

<dependency>
  <groupId>xerces</groupId>
  <artifactId>xercesImpl</artifactId>
  <version>2.11.0</version>
</dependency>

I quess 2.11.0 should be newer than 2.11.0.beta, but it seems like xsd1.1 is not supported in that version !我怀疑 2.11.0 应该比 2.11.0.beta 更新,但该版本似乎不支持 xsd1.1 !

Instead only the following dependency lead to a working XSD1.1 validation for me:相反,只有以下依赖项会为我带来有效的 XSD1.1 验证:

<dependency>
    <groupId>org.opengis.cite.xerces</groupId>
    <artifactId>xercesImpl-xsd11</artifactId>
    <version>2.12-beta-r1667115</version>
</dependency>

( Found in this SO thread: How to validate XML against XSD 1.1 in Java? ) (在这个 SO 线程中找到: How to validate XML against XSD 1.1 in Java?

I think they have added version 2.11 to maven now.我认为他们现在已经向 Maven 添加了 2.11 版。 The following dependency in Maven works out-of-the-box: Maven 中的以下依赖项开箱即用:

<dependency>
  <groupId>xerces</groupId>
  <artifactId>xercesImpl</artifactId>
  <version>2.11.0</version>
</dependency>

Xerces-J provides a "fully compliant XML Schema 1.1 implementation" since version 2.12.0, see release history here: https://xerces.apache.org/news.html . Xerces-J 自 2.12.0 版起提供“完全兼容的 XML Schema 1.1 实现”,请参阅此处的发布历史: https ://xerces.apache.org/news.html。

Xerces2 Java 2.12.2 (XML Schema 1.1) - tar.gz [PGP] [SHA] Just got released earlier this week. Xerces2 Java 2.12.2 (XML Schema 1.1) - tar.gz [PGP] [SHA] 本周早些时候刚刚发布。 https://xerces.apache.org/mirrors.cgi#notes https://xerces.apache.org/mirrors.cgi#notes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM