0

以下程序将 schemaLanguage 设置为“ http://www.w3.org/XML/XMLSchema/v1.1 ”,newSchema() 返回类型为 {org.apache.xerces.jaxp.validation.SimpleXMLSchema} 的 Schema。我无法导入类,错误是 - org.apache.xerces.jaxp.validation.SimpleXMLSchema 类型不可见

我的意图是将 XSD(Ver 1.1)断言值(如下所示)解析为 XPath 表达式,并且在 SimpleXMLSchema 对象中可用。

Example: <assert test="starts-with(@partnumber,../@partnumber)"/>

有没有其他方法可以获取 XSD1.1 Schema 对象?

使用的jar:xercesImpl-xsd11-2.12-beta-r1667115.jar、org.eclipse.wst.xml.xpath2.processor-2.1.100.jar

任何建议/帮助都将帮助我解决问题。谢谢。

/*
 * Xsd11SchemaValidator.java
import javax.xml.validation.SchemaFactory;*/
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import javax.xml.validation.Validator;
import java.io.*;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
import org.apache.xerces.impl.xs.SchemaGrammar;
import org.apache.xerces.jaxp.validation.*;

class Xsd11SchemaValidator {
  private static int errorCount = 0;
  public static void main() {
      String schemaName = "Path to XSD 1.1 File";;

      Schema schema = loadSchema(schemaName);

    }
  }

  public static Schema loadSchema(String name) {
    Schema schema = null;
    try {
      String language = "http://www.w3.org/XML/XMLSchema/v1.1";
      SchemaFactory factory = SchemaFactory.newInstance(language);
      schema = factory.newSchema(new File(name));
    } catch (Exception e) {
      System.out.println(e.toString());
    }
    return schema;
  }
}
4

1 回答 1

0

官方的 xerces-version 似乎还不支持 xsd1.1。但是,以下 Maven 依赖项对我来说效果很好:

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

这里有一些示例代码来解析 v1.1。xsd:

import java.io.File;
import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

...

private static void validateFile(File xmlFile, File xsdFile) throws SAXException, IOException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
    File schemaLocation = xsdFile;
    Schema schema = factory.newSchema(schemaLocation);
    Validator validator = schema.newValidator();
    Source source = new StreamSource(xmlFile);
    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());
    } }
于 2016-03-14T09:42:46.377 回答