按照程序设置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;
}
}
答案 0 :(得分:0)
官方的xerces-version似乎还不支持xsd1.1。但是,以下maven-dependency对我来说效果很好:
<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());
} }