Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Schema Validation Filter

The Schema Validation filter takes XML inputs and validates these against a referenced XSD schema.

Typically, you can place this filter before another message processor, such as a connector, 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.

Minimum Configuration: The path to the XSD schema location.

The Schema Validation Filter uses the JAXP libraries to validate a message against a schema. You must provide the path, file name, and extension of the schema or schemas in the Schema Locations property.

Optionally, you can set Schema Language to the schema language to use. If not specified, the property defaults to XML Schema.

In the case 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 determinate the behavior.

Basic Example

By default, the Schema Validation Filter only checks that the XML content matches the referenced schema validation, but it doesn’t pass this content onwards to the next element of your flow


         
      
1
2
3
4
5
6
7
8
<flow name="case11843-crFlow1" doc:name="case11843-crFlow1">
        <file:inbound-endpoint path="/File/" responseTimeout="10000" doc:name="File" />
        <file:file-to-string-transformer doc:name="File to String" />
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />
        <mulexml:schema-validation-filter schemaLocations="OrderQuoteData.xsd" returnResult="true" name="OrderData_Schema_Validation"
        doc:name="Schema Validation" />
        <logger message="FILE OK: #[message.payload]" level="INFO" doc:name="Logger" />
    </flow> 

Full Code


          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
 
    <flow name="case11843-crFlow1" doc:name="case11843-crFlow1">
        <file:inbound-endpoint path="/File/" responseTimeout="10000" doc:name="File" />
        <file:file-to-string-transformer doc:name="File to String" />
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />
        <mulexml:schema-validation-filter schemaLocations="OrderQuoteData.xsd" returnResult="true" name="OrderData_Schema_Validation"
        doc:name="Schema Validation" />
        <logger message="FILE OK: #[message.payload]" level="INFO" doc:name="Logger" />
    </flow>
</mule>
Note that in this example, even when the XML content of the file matches the XSD format, the payload that is passed on forward is null. After validating that the content is correct, it is not passed on forward.

Passing the XML Forward

To allow a valid XML to pass the filter, 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 retains the XML content. 


         
      
1
2
3
4
5
6
7
<flow name="case11843-crFlow1" doc:name="case11843-crFlow1">
  <file:inbound-endpoint path="/File/" responseTimeout="10000" doc:name="File" />
  <file:file-to-string-transformer doc:name="File to String" />
  <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />
  <mulexml:schema-validation-filter schemaLocations="OrderQuoteData.xsd" returnResult="false" name="OrderData_Schema_Validation" doc:name="Schema Validation" />
  <logger message="FILE OK: #[message.payload]" level="INFO" doc:name="Logger" />
</flow> 

Full Code

(Collapsed content)


          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
 
    <flow name="case11843-crFlow1" doc:name="case11843-crFlow1">
        <file:inbound-endpoint path="/File/" responseTimeout="10000" doc:name="File" />
        <file:file-to-string-transformer doc:name="File to String" />
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />
        <mulexml:schema-validation-filter schemaLocations="OrderQuoteData.xsd" returnResult="false" name="OrderData_Schema_Validation"
        doc:name="Schema Validation" />
        <logger message="FILE OK: #[message.payload]" level="INFO" doc:name="Logger" />
    </flow>
</mule>

Inserting the Validator into a Filter

By default, when the XML content doesn’t match the XSD validation, an exception is raised. If you want to instead process the rejected messages separately, you can create the schema validator as a global element and then reference it from inside a filter. Then, you can take advantage of the onUnaccepted or the throwOnUnaccepted properties of the filter to direct the processing of the message to a different flow when the conditions of the filter aren’t met, such as when the XML is not validated.


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<mulexml:schema-validation-filter schemaLocations="OrderQuoteData.xsd" returnResult="false" name="OrderData_Schema_Validation"
        doc:name="Schema Validation" />
 
    <flow name="case11843-crFlow1" doc:name="case11843-crFlow1">
        <file:inbound-endpoint path="/File/" responseTimeout="10000" doc:name="File" />
        <file:file-to-string-transformer doc:name="File to String" />
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />
        <message-filter onUnaccepted="error_invalid_data" doc:name="Validate Order Xml" />
        <logger message="FILE OK: #[message.payload]" level="INFO" doc:name="Logger" />
    </flow>
 
    <flow name="error_invalid_data" doc:name="error_invalid_data">
        <logger message="ERROR: MESSAGE PAYLOAD: #[message.payload]" level="INFO" doc:name="Logger" />
    </flow>

Full Code


          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
 
    <mulexml:schema-validation-filter schemaLocations="OrderQuoteData.xsd" returnResult="false" name="OrderData_Schema_Validation"
        doc:name="Schema Validation" />
 
    <flow name="case11843-crFlow1" doc:name="case11843-crFlow1">
        <file:inbound-endpoint path="/File/" responseTimeout="10000" doc:name="File" />
        <file:file-to-string-transformer doc:name="File to String" />
        <mulexml:dom-to-xml-transformer doc:name="DOM to XML" />
        <message-filter onUnaccepted="error_invalid_data" doc:name="Validate Order Xml" />
        <logger message="FILE OK: #[message.payload]" level="INFO" doc:name="Logger" />
    </flow>
 
    <flow name="error_invalid_data" doc:name="error_invalid_data">
        <logger message="ERROR: MESSAGE PAYLOAD: #[message.payload]" level="INFO" doc:name="Logger" />
    </flow>
</mule>

In the example above, when the XML content doesn’t match the XSD validation, the message directs instead to the flow named error_invalid_data.

Note that, as the returnResult attribute is set to false in the global element, the rejected messages that are sent to the other flow also includes the XML content. In case that the returnResult attribute were set to true, the payload of the rejected messages sent to this flow would be null.