在 XSD 验证错误时获取父元素

Get parent element on XSD validation error

我有以下 xml 带有单位和度量子元素。

<Depth>
    <measure>1.00</measure>
    <unit>in</unit>
<Depth>
<Width>
    <measure>1.00</measure>
    <unit>in</unit>
</Width>
<vendorPackHeight>
    <measure>1.00</measure>
    <unit>in</unit>
</vendorPackHeight>
<Weight>
    <measure>7.00</measure>
    <unit>LBS</unit> //invalid expected value is lb
</Weight> 

当 XSD 单位或度量子元素的验证失败并出现类似 cvc-enumeration-valid 的错误时,当度量不是来自一组枚举值时或单位值因 cvc-datatype-valid 而失败。 1.2.1 当数据类型不匹配时如何获取父元素?在上面的 xml 中,它将是重量。

在 SAXParseException 中,我得到了发生错误的行号。是否可以从行号中获取元素,然后获取其父元素?

我认为 Java API 中没有执行此操作的标准方法。但是,某些库确实允许您偷看它当前在哪个元素上。例如,在 Apache Xerces 实现中,它支持通过

获取当前节点

getProperty("http://apache.org/xml/properties/dom/current-element-node")

在他们的网站上查看有关 属性 的文档:https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node


Xerces library 默认由您的 JDK 提供,但也可以作为第 3 方库导入到您的项目中。如果您必须为 运行 您的应用程序正确使用它,我建议添加它。下面是一些示例代码,可将 XML 文档验证为 XSD 并获取当前节点。

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.xerces.impl.Constants;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;

public class XSDTest {

    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {

        // our XSD, which defines 1 node  <TheNode> which must have decimal text content 
        byte [] schemaData = ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
                    + "<xs:simpleType name=\"theNodeType\">"
                        + "<xs:restriction base=\"xs:decimal\"/>"
                    + "</xs:simpleType>"
                    + "<xs:element name=\"TheNode\" type=\"theNodeType\"/>"
                + "</xs:schema>").getBytes();
        // our invalid xml
        byte [] xmlData = "<TheNode>123NotADecimal</TheNode>".getBytes();

        // parse schema
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaSource = new StreamSource(new ByteArrayInputStream(schemaData));
        Schema schema = schemaFactory.newSchema(schemaSource);

        // build our document, must use document builder to enable xerces parser properties for DOM
        // Also must be a xerces implementation of the DBF, should be enabled by default in a standard java project but just to be verbose about it
        // pass in the full name of the DBF impl
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.class.getName(), XSDTest.class.getClassLoader());
        dbf.setNamespaceAware(true);
        Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xmlData));


        // configure our validator and parse the document.
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new MyErrorHandler(validator));
        validator.validate(new DOMSource(doc.getDocumentElement()));
    }

    private static class MyErrorHandler implements ErrorHandler {
        private final Validator xsdValidator;

        public MyErrorHandler(Validator xsdValidator) {
            this.xsdValidator = xsdValidator;
        }
        @Override
        public void warning(SAXParseException exception) throws SAXException {
            System.out.println("Warning on node: " + getCurrentNode());
            System.out.println(exception.getLocalizedMessage());
        }

        @Override
        public void error(SAXParseException exception) throws SAXException {
            System.out.println("Error on node: " + getCurrentNode());
            System.out.println(exception.getLocalizedMessage());
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            System.out.println("Fatal on node: " + getCurrentNode());
            System.out.println(exception.getLocalizedMessage());
        }


        private Node getCurrentNode() throws SAXNotRecognizedException, SAXNotSupportedException {
            // get prop "http://apache.org/xml/properties/dom/current-element-node"
            // see https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node
            return (Node)xsdValidator.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY);
        }
    }

}

输出:

Error on node: [TheNode: null]
cvc-datatype-valid.1.2.1: '123NotADecimal' is not a valid value for 'decimal'.
Error on node: [TheNode: null]
cvc-type.3.1.3: The value '123NotADecimal' of element 'TheNode' is not valid.

如果您切换到 Saxon 作为您的模式验证器,您将能够找到此信息。

当您使用 Saxon 验证内存中的树时,ValidationFailure 对象包含对验证失败的实际节点的引用(显然您可以从那里导航到父节点)。

当您使用 Saxon 验证 SAX 流时,ValidationFailure 对象包含一个 Path,它是一个结构化对象,包含有关发生故障的节点及其每个祖先的信息。每个祖先可用的信息包括节点种类、节点名称和兄弟节点位置。