我正在研究模式验证。目标是获取 XSD 文件并根据它验证传入文档。如果有错误,我想捕获所有错误。
我只在 ErrorHandler 中收到第一个错误,然后处理结束。互联网上有很多人们问同样问题的例子,答案似乎总是我正在做的事情(创建一个自定义错误处理程序)。
此外,ErrorHandler 接口的文档有这样的说明错误方法应该如何工作:
/**
* <p>The SAX parser must continue to provide normal parsing
* events after invoking this method: it should still be possible
* for the application to process the document through to the end.
* If the application cannot do so, then the parser should report
* a fatal error even if the XML recommendation does not require
* it to do so.</p>
*/
请注意,这是一个 Java 13 示例,但没有理由确实需要如此(除了简洁的 xml 文本定义)。
private String drugValidationSchema = """
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.company.com/Drug"
xmlns:apins="https://www.company.com/Drug" elementFormDefault="qualified">
<element name="drugRequest" type="apins:drugRequest"></element>
<element name="drugResponse" type="apins:drugResponse"></element>
<complexType name="drugRequest">
<sequence>
<element name="id" type="int"></element>
</sequence>
</complexType>
<complexType name="drugResponse">
<sequence>
<element name="id" type="int"></element>
<element name="drugClass" type="string"></element>
<element name="drugName" type="string"></element>
</sequence>
</complexType>
</schema>
""";
// This document has 3 errors in it based on the schema above:
// 1) idx instead of id
// 2) dugClass instead of drugClass
// 3) dugName instead of drugName
private String badDrugResponseXml = """
<?xml version="1.0" encoding="UTF-8"?>
<apins:drugResponse xmlns:apins="https://www.company.com/Drug" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.company.com/Drug Drug.xsd ">
<apins:idx>1</apins:idx>
<apins:dugClass>opioid</apins:dugClass>
<apins:dugName>Buprenorphine</apins:dugName>
</apins:drugResponse>
""";
/**
* This test does nothing but send the desired files into the validation
* process. The goal is for the validation process to output 3 errors.
* For reasons I don't understand, it will only output the first one and
* stop the processing.
*/
@Test
void testWithValidator() {
System.out.println("Test an entry with multiple errors: " + validateXMLSchema(drugValidationSchema, badDrugResponseXml));
Assertions.assertTrue(true);
}
/**
* This validator process seems to always stop after the first error is encountered.
*
* @param xsdPath the actual XSD content as String
* @param xmlPath the actual xml document text as String.
* @return True if there are no errors, false otherwise. (planning to return details)
*/
static boolean validateXMLSchema(String xsdPath, String xmlPath){
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(new StringReader(xsdPath)));
Validator validator = schema.newValidator();
List<Exception> exceptions = new ArrayList<>();
// Add a custom handler to the validator. The goal is to continue processing
// the document so that ALL errors are captured.
validator.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) {
exceptions.add(exception);
}
@Override
public void fatalError(SAXParseException exception) {
exceptions.add(exception);
}
@Override
public void error(SAXParseException exception) {
exceptions.add(exception);
}
});
validator.validate(new StreamSource(new StringReader(xmlPath)));
if (exceptions.size() > 0) {
for (Exception ex : exceptions) {
System.out.println("Error found: " + ex.getMessage());
}
}else {
System.out.println("No errors.");
}
} catch (SAXException | IOException e) {
System.out.println("Exception: "+e.getMessage());
return false;
}
return true;
}
正如注释所示,在调试中很明显,第一个错误是通过自定义错误处理程序的结果报告的,但处理不会继续并找到后续的两个错误。
答案并不简单,但请耐心等待……
我与一个实现了完全兼容的验证 XML 解析器的团队合作。我向他们询问了这个功能。他们解释说,不正确/意外的标签名称(同一件事)可能由两种情况导致:
a) xsd 中正确位置的标签名称不正确
b) 纠正 xsd 中错误位置的标签名称
当人们要求此功能时,他们几乎总是想到场景 a)。 XSD 非常简单(XML 文档中的可变性非常有限),并且对于人类读者来说,意外的标签名称“显而易见”是一个拼写错误。不幸的是,XSD 规范允许多种类型的变化。您可以有 xs:any(通配符)、选择组、无序组、可选元素、具有各种类型限制的复杂类型扩展等。如果 XSD 非常“开放”,则根本不明显的是意外的标记名称是一个简单的错字。尝试继续将毫无意义在一般情况下因为 XML 解析器不知道从哪里继续解析。
只有一种情况 XML 处理器可以发出验证错误并且safely继续解析在所有情况下。当。。。的时候简单值标签/属性的内容不符合 xsd:facet 限制,可以报告错误并继续。解析器并没有丢失 XSD 中的“上下文”,因为元素的名称都已成功匹配。
您可能会想参考您的示例并说“但就我而言,解析可以安全地继续”。您是对的,但我不知道有任何 XML 解析器能够区分不匹配的标记名称的“安全继续”和“不安全继续”情况。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)