针对多个XSD的条件XML验证-忽略某些XSD的验证错误,仅对某些XSD的验证失败

针对多个XSD的条件XML验证-忽略某些XSD的验证错误,仅对某些XSD的验证失败,xsd,xsd-validation,xml-validation,Xsd,Xsd Validation,Xml Validation,背景:从另一个系统使用XML。解析、验证并保存到自己的数据库中。 由于并非所有字段都与我的系统相关,因此架构验证仅适用于某些元素类型。如果验证失败,则终止执行。剩余类型即使验证失败,仍将继续在数据库中持久化 我正在根据XSD列表验证XML(一个主XSD,包含多个XSD)。目前在我的代码中,验证器正在检查所有包含的XSD的一致性。 然而,要求是-对某些XSD必须进行模式验证。对于其他一些情况,即使XML元素类型不符合XSD类型,也不要失败。如何做到这一点 e、 例如,Person.xsd和Prod

背景:从另一个系统使用XML。解析、验证并保存到自己的数据库中。
由于并非所有字段都与我的系统相关,因此架构验证仅适用于某些元素类型。如果验证失败,则终止执行。
剩余类型即使验证失败,仍将继续在数据库中持久化

我正在根据XSD列表验证XML(一个主XSD,包含多个XSD)。目前在我的代码中,验证器正在检查所有包含的XSD的一致性。
然而,要求是-对某些XSD必须进行模式验证。对于其他一些情况,即使XML元素类型不符合XSD类型,也不要失败。如何做到这一点

e、 例如,Person.xsd和Product.xsd对于模式验证是必需的。可以忽略Address.xsd和Phone.xsd中的验证

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="com.demo.devika" xmlns:tns="com.demo.devika" elementFormDefault="qualified">
    <xsd:include schemaLocation="Address.xsd" />
    <xsd:include schemaLocation="Phone.xsd" />
    <xsd:include schemaLocation="Product.xsd" />

    <xsd:element name="Person" type="tns:Person_Type"/>
    <xsd:complexType name="Person_Type">
        <xsd:sequence>
            <xsd:element name="FirstName" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="LastName" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="Address" type="tns:Address_Type" minOccurs="1" maxOccurs="unbounded"/>
            <xsd:element name="Phone" type="tns:Phone_Type" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="Product" type="tns:Product_Type" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

XML验证方法

public boolean execute(InputStream inputXML) throws XMLStreamException, SAXException, IOException {

        // Creating Source array for Schema
        List<Source> schemaSources = schemaFiles.stream().map(schemaFile -> new StreamSource(schemaFile))
                .collect(Collectors.toCollection(ArrayList::new));

        try {
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            schemaFactory.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACE_GROWTH_FEATURE,
                    Boolean.TRUE);

            Schema schemaGrammar = schemaFactory
                    .newSchema(schemaSources.toArray(new StreamSource[schemaSources.size()]));

            Validator schemaValidator = schemaGrammar.newValidator();

            schemaValidator.validate(new StreamSource(inputXML));
            return true;

        } catch (SAXException | IOException ex) {
            throw ex;
        } finally {
            inputXML.close();
        }

    }
公共布尔执行(InputStream inputXML)抛出XMLStreamException、SAXException、IOException{ //为架构创建源数组 List schemaSources=schemaFiles.stream().map(schemaFile->newstreamsource(schemaFile)) .collect(收集器.toCollection(ArrayList::new)); 试一试{ SchemaFactory SchemaFactory=SchemaFactory.newInstance(xmlstants.W3C\u XML\u SCHEMA\u NS\u URI); schemaFactory.setFeature(Constants.XERCES\u FEATURE\u PREFIX+Constants.NAMESPACE\u GROWTH\u FEATURE, 布尔值(TRUE); Schema schemaGrammar=schemaFactory .newSchema(schemaSources.toArray(新的StreamSource[schemaSources.size()]); Validator schemaValidator=schemaGrammar.newValidator(); 验证(新的StreamSource(inputXML)); 返回true; }捕获(SAXException | IOException ex){ 掷骰子; }最后{ inputXML.close(); } }