简体   繁体   中英

Java XML validation Ignore xml elements that do not belong to schema

I am working on a Spring Java application that interacts with a SOAP web service. We have enabled strict validation for responses (using an interceptor as shown below), this is required to ensure that already agreed mandatory elements are not missed in response.

public class MyPayloadValidatingInterceptor extends org.springframework.ws.client.support.interceptor.PayloadValidatingInterceptor{

    @Override
    protected boolean handleResponseValidationErrors(final MessageContext messageContext,
            final SAXParseException[] errors) {
        for (SAXParseException error : errors) {
            this.logger.error("XML validation error in SOAP response: " + error.getMessage());
        }
        throw new MyResponseValidationException(errors);
    }
}

This is making our application fragile even for small changes to the Schema (new elements in response) at the SOAP web service and forcing client to upgrade the schema even though we don't use the new elements, application Not forward compatible. I want a way to ignore the unidentified element in the response xml. While having strict validation for the mandatory elements that are part of the current schema that my client application is using.

For example if schema the is

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Employee" xmlns:tns="http://www.example.org/Employee"
    elementFormDefault="qualified">
    <element name="Employee">
        <complexType>
            <sequence>
                <element name="EmployeeId" type="string"></element>
                <element name="Name" type="string"></element>
                <element name="SecondName" type="string" minOccurs="0"></element>
            </sequence>
        </complexType>
    </element>
</schema>

For the below xml I want the validation interceptor to ignore the unidentified element, No MyResponseValidationException

<tns:Employee xmlns:tns="http://www.example.org/Employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/Employee Employee.xsd ">
  <tns:EmployeeId>id</tns:EmployeeId>
  <tns:Name>Jack</tns:Name>
  <tns:NewElement>unidentified</tns:NewElement>
  <tns:SecondName>Second Jack</tns:SecondName>
</tns:Employee>

Validation ERROR I see is, cvc-complex-type.2.4.a: Invalid content was found starting with element 'tns:NewElement'. One of '{" http://www.example.org/Employee ":SecondName}' is expected.

For this xml it should result in MyResponseValidationException

<tns:Employee xmlns:tns="http://www.example.org/Employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/Employee Employee.xsd ">
  <tns:EmployeeId>tns:EmployeeId</tns:EmployeeId>
  <!--tns:Name>tns:Name</tns:Name Mandatory Element missing-->  
  <tns:SecondName>tns:SecondName</tns:SecondName>
</tns:Employee>

Validation ERROR I see is cvc-complex-type.2.4.a: Invalid content was found starting with element 'tns:SecondName'. One of '{" http://www.example.org/Employee ":Name}' is expected.

Can you please suggest best way to differentiate between missing elements and elements that do not belong to schema.

Thanks in advance.

I think this is impossible - you've added strict validation but asking for nonstrict and to bypass unknown elements at the same time.

Maybe you need to add some kind of

<xs:any processContents="skip" minOccurs="0"/>

at the end of schema and then ask clients to add all unknown elements after all known.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM