即使使用 ErrorHandler,为什么模式验证在第一个错误后结束?

Even with ErrorHandler, why is schema validation ending after the first error?

我正在研究模式验证。目标是获取一个 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 解析器。我向他们询问了这个确切的功能。他们解释说 incorrect/unexpected 标签名称(相同的东西)可能来自两种情况:

a) xsd

中正确位置的标签名称不正确

b) xsd

中错误位置的正确标签名称

当人们要求这个功能时,他们几乎总是想到场景 a)。 XSD 非常简单(XML 文档中的可变性非常有限),对于人类来说 'obvious' reader 意外的标签名称是错字。不幸的是,XSD 规范允许多种类型的可变性。您可以有 xs:any(通配符)、选择组、无序组、可选元素、具有各种类型限制的复杂类型扩展等。如果 XSD 非常 'open' 那么它不在很明显,意外的标签名称是一个简单的拼写错误。在一般情况下尝试继续是没有意义的,因为XML解析器不知道从哪里继续解析。

只有一种情况 XML 处理器可以发出验证错误并且 安全地 继续解析 在所有情况下 .当tag/attribute的简单值不符合xsd:facet限制时,报错继续即可。解析器没有在 XSD 中丢失它的 'context' 因为元素的名称已经全部匹配成功。

您可能想参考您的示例并说 'but in my case, parsing could safely continue'。你是对的,但我不知道有哪个 XML 解析器能够区分 'safe to continue' 和 'unsafe to continue' 标签名称不匹配的情况。