- 我当前的验证不适用于 XSD v1.1 Schemas .. 我尝试了很多方法来改变它,但直到现在都没有成功
- 解决方案是用 Saxon 还是 Xerces 完成对我来说并不重要(编辑:我不想花钱来解决问题,而且看起来 Saxon XSD1.1 验证不是免费的,所以我想我必须坚持赛斯)
- 是的,我已经为此搜索过,但到目前为止,没有任何代码片段可以帮助我获得有效的验证。
- 该代码将在 Eclipse 插件中使用,如果这很重要的话
- 我将以下 jar 文件添加到项目/类路径中,但看起来它没有在我的代码中使用:
<dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.11.0</version> </dependency>
这是我迄今为止用于验证的代码(如果不能用于 xsd1.1,则转储它没有问题):
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.xni.parser.XMLInputSource;
....
public List<MyError> validate(File xmlFile) {
List<MyError> errors = null;
try {
DOMParser parser = new DOMParser();
parser.setFeature(XmlUtils.VALIDATION, true);
parser.setFeature(XmlUtils.VALIDATION_SCHEMA, true);
parser.setFeature(XmlUtils.ALL_SCHEMA_LOCATIONS, true);
parser.setFeature(XmlUtils.DEFER_NODE_EXPANSION, false);
Handler handler = new Handler(xmlFile, parser);
parser.setErrorHandler(handler);
// there probably are smarter ways to do this
String uri = xmlFile.toURI().toString().replace("%20", " ");
InputStream inputStream = new FileInputStream(xmlFile);
XMLInputSource inputSource = new XMLInputSource("", uri, uri, inputStream, "UTF-8");
parser.parse(inputSource);
errors = handler.getErrors();
}
catch (Exception e)
{
ConsoleHandler.printError("Document " + xmlFile.getName() + " has not been parsed correctly: " + e.getMessage(), true);
ConsoleHandler.printStackTrace(e);
}
// printing the errors happens in some other method
return errors;
}