2015-10-25 60 views
0

以下程序集的schemaLanguage爲「http://www.w3.org/XML/XMLSchema/v1.1」,newSchema()返回{org.apache.xerces.jaxp.validation.SimpleXMLSchema}類型的Schema。 即─類型org.apache.xerces.jaxp.validation.SimpleXMLSchema是不可見的我不能導入類,錯誤如何獲取XSD1.1模式解析?

我的目的是解析XSD(版本1.1)斷言值(如下所示)作爲XPath表達式,它在SimpleXMLSchema對象中可用。

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

是否有任何其他方式獲取XSD1.1 Schema對象?

罐使用:xercesImpl-xsd11-2.12-β-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; 
    } 
} 

回答

0

官方的xerces版本似乎還不支持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()); 
    } }