【问题标题】:How to get XSD1.1 Schema Model for parsing?如何获取 XSD1.1 Schema Model 进行解析?
【发布时间】:2016-01-24 19:50:18
【问题描述】:

以下程序集的 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;
  }
}

【问题讨论】:

    标签: xml xslt xpath xsd-1.1


    【解决方案1】:

    官方的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());
        } }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多