Schema Validation Filter (XML Schema Validation)


The Schema Validation filter takes XML inputs and validates these against a referenced XSD schema. Typically, we’ll place it at the start of the Mule flow to determine whether the incoming message or event should be handled or not, allowing the message to only continue on along the flow when the filter validations are met and the included XML is considered valid. Schema Validator is provided out of the box with Mule.

Use Case

A common use case in Mule ESB flows is validating if an XML document is valid against a corresponding XSD, so we can make sure we are receiving the correct XML, and will forward it for the further processing.


<mulexml:schema-validation-filter schemaLocations="src/main/resources/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation" />

The “standard” way of Validating XML schema in Mule is by using the schema-validation-filter and especially in combination with the message-filter.
In a case when you want to get an exception and/or call another flow when the validation fails, you could wrap the schema validation filter inside a message filter and then use “onUnaccepted” and “throwOnUnaccepted” configuration attributes to determine the behavior.
Here we have wrapped the schema-validation-filter in a message-filter and set throwOnUnaccepted to “true.”


<message-filter throwOnUnaccepted="true" >
    <mulexml:schema-validation-filter schemaLocations="/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation" />

Note: the schemaLocations attribute accepts a comma separated list of schemas to validate the payload against.

Passing the XML Forward

In order to allow a valid XML to pass the filter, you need to set the returnResult to false in your schema-validation-filter. This way, the payload of the message that is passed onwards to the next element in your flow will retain the XML content.
Let’s walk through how to use All Validator in a Mule application. In this example, we are receiving the XML file through file connector, which will be validated against an XSD schema file defined in the application. If validation fails, then we catch the exception and print the “Schema Not Validated” message along with the exception.


Schema Validation Flow
Schema Validation Flow


<mule xmlns:encryption="" xmlns:filters="" xmlns:mulexml="" xmlns:file="" xmlns:json="" xmlns:http="" xmlns="" xmlns:doc="" xmlns:spring="" xmlns:xsi="" xsi:schemaLocation="">
    <flow name="SchemaValidationFLow" >
        <file:inbound-endpoint path="src/main/resources/File/Process/input" responseTimeout="10000" doc:name="File" moveToDirectory="src/main/resources/File/Process/output" >
          <file:filename-wildcard-filter pattern="*.xml" caseSensitive="false"/>
        <file:file-to-string-transformer doc:name="File to String" />
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />    
       <message-filter throwOnUnaccepted="true" doc:name="Message">
           <mulexml:schema-validation-filter schemaLocations="src/main/resources/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation" />
        <logger message="Validation successful: #[message.payload]" level="INFO" doc:name="Logger" />
       <catch-exception-strategy doc:name="Catch Exception Strategy">
       <logger level="INFO" message="Schema Not Validated" doc:name="Logger"/>

XSD Schema src/main/resources/File/Process/Schema.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="">
<xs:element name="shiporder">
       <xs:element name="orderperson" type="xs:string"/>
       <xs:element name="shipto">
             <xs:element name="name" type="xs:string"/>
             <xs:element name="address" type="xs:string"/>
             <xs:element name="city" type="xs:string"/>
             <xs:element name="country" type="xs:string"/>
       <xs:element name="item" maxOccurs="unbounded">
             <xs:element name="title" type="xs:string"/>
             <xs:element name="note" type="xs:string" minOccurs="0"/>
             <xs:element name="quantity" type="xs:positiveInteger"/>
             <xs:element name="price" type="xs:decimal"/>
     <xs:attribute name="orderid" type="xs:string" use="required"/>

Now in the above XML if we change the any of the element aganist the schema defined ,we will be getting the below exception.

<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923" xmlns:xsi="" xsi:noNamespaceSchemaLocation="shiporder.xsd">
   <orderperson>John Smith</orderperson>
     <name>Ola Nordmann</name>
<address>Langgt 23</address>

     <city>4000 Stavanger</city>
     <title>Empire Burlesque</title>
     <note>Special Edition</note>
     <title>Hide your heart</title>


ERROR 2017-08-08 21:01:19,585 [[schemavalidation1].SchemaValidationFLow.stage1.02] org.mule.exception.CatchMessagingExceptionStrategy: 
Message               : Message has been rejected by filter.
Filter                : org.mule.module.xml.filters.SchemaValidationFilter@39c5e4bb (null)
Payload               : <?xml version="1.0" encoding="UTF-8"?>
Element XML           : <message-filter throwOnUnaccepted="true" doc:name="Message">
                        <mulexml:schema-validation-filter schemaLocations="src/main/resources/File/Process/Schema.xsd" returnResult="true" name="OrderData_Schema_Validation" doc:name="Schema Validation"></mulexml:schema-validation-filter>
Payload Type          : org.apache.xerces.dom.DocumentImpl
Element               : /SchemaValidationFLow/processors/2 @ schemavalidation1:schemavalidation1.xml:23 (Message)
Root Exception stack trace:
org.mule.api.routing.filter.FilterUnacceptedException: Message has been rejected by filter.

Previous articleActiveMQ Read and Write Flow in Mule
Ankit has 6+ years of experience in design and development of web based applications in Java/JEE. He lately develpoed his interest in ESB Mule. His great passion and interest in this field guided him to become certified Mule Developer. Ankit is passionate about stock market and technology. He hails from beautiful Rajasthan in India and currently lives in Gurgaon with his family.