3
votes

I need to validate incoming XML file based on XSD Schema. Is there any component to achieve tis task? How can I achieve this in mule esb?

Please suggest I'm bit new to mule., Thanks..,

4

4 Answers

1
votes

You can use the following example :-

 <mulexml:schema-validation-filter schemaLocations="yourSchema.xsd" returnResult="true" name="Schema_Validation"
        doc:name="Schema Validation" />

and can refer your .xsd file against which your incoming XML need to be validate. Mule has a validation filter which will do the validation for you ..

reference :- https://docs.mulesoft.com/mule-user-guide/v/3.7/schema-validation-filter

1
votes

reference :- https://docs.mulesoft.com/mule-user-guide/v/3.7/schema-validation-filter

seems not working here as for the code snippet as follows the error as below still is being received.

Code snippet used as per documentation:

 <message-filter throwOnUnaccepted="true" doc:name="Message">
                            <mulexml:schema-validation-filter schemaLocations="GetMasterOrganizationRecordByIDRequest.xsd" returnResult="false"/>
                        </message-filter>

The schema used for validation as below:

    <xs:element name="CustMDGetRequest" type="CustMDGetRequestType"/>

    <xs:complexType name="CustMDGetRequestType">
        <xs:sequence>  
            <xs:element name="CallingSourceSystem" type="xs:string" maxOccurs="1" minOccurs="1"/>
            <xs:element name="SourceSystemId" type="xs:string" maxOccurs="1" minOccurs="1"/>
            <xs:element name="SourceSystemNm" type="xs:string" maxOccurs="1" minOccurs="1"/>
            <xs:element name="IncludeDefaultAttributesForSourceTypes" type="xs:boolean" maxOccurs="1" minOccurs="0"/>
            <xs:element name="SourceTypesToBeIncluded" type="xs:string" maxOccurs="1" minOccurs="0"/>
            <xs:element name="Filters" type="FiltersType" maxOccurs="unbounded" minOccurs="0"/>
            <xs:element name="AsynchronousRequestId" type="xs:string" maxOccurs="1" minOccurs="0"/> 
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="FiltersType">
        <xs:sequence>
            <xs:element name="Name" type="xs:string" maxOccurs="1" minOccurs="1"/>
            <xs:element name="Values" type="xs:string" maxOccurs="1" minOccurs="1"/>
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Error received:

org.mule.api.routing.filter.FilterUnacceptedException: Message has been rejected by filter.


------
ERROR 2017-05-24 21:01:14,887 [[eip-vcits-mdm-processes-proxy].HTTP_Listener_Configuration1.worker.01] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : Message has been rejected by filter.
Payload               : <?xml version="1.0" encoding="UTF-8"?>
                        <CustMDGetRequest>
                                                <CallingSourceSystem>AUTOPUBLISH</CallingSourceSystem>
                                                <SourceSystemId>348109</SourceSystemId>
                                                <SourceSystemNm>CUSTMD</SourceSystemNm>
                                                <IncludeDefaultAttributesForSourceTypes>true</IncludeDefaultAttributesForSourceTypes>
                            </CustMDGetRequest>

Payload Type          : java.lang.String
Filter                : org.mule.module.xml.filters.SchemaValidationFilter@435baa89 (null)
Element               : /eip-vcits-mdm-processes-proxy-templateFlow/processors/1/0/1/0/3 @ eip-vcits-mdm-processes-proxy:eip-vcits-mdm-processes-proxy.xml:43 (Message)
Element XML           : <message-filter throwOnUnaccepted="true" doc:name="Message">
                        <mulexml:schema-validation-filter schemaLocations="GetMasterOrganizationRecordByIDRequest.xsd" returnResult="false"></mulexml:schema-validation-filter>
                        </message-filter>
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.routing.filter.FilterUnacceptedException: Message has been rejected by filter.
    at org.mule.routing.MessageFilter.filterUnacceptedException(MessageFilter.java:113)
1
votes

The schema validation with the .xsd didn't work for me but the validation with regex worked for me as show below for above stated problem statement.

<set-variable variableName="CallingSrcSys" value="#[xpath3('//CallingSourceSystem')]" doc:name="Variable-callingSrcSystem"/>
                <validation:matches-regex value="#[flowVars.CallingSrcSys]" regex="^[a-zA-Z]{0,14}+$" doc:name="Validation-CallingSrcSys"/>
                <set-variable variableName="MandatoryFieldRestrictionLevel" value="#[xpath3('//MandatoryFieldRestrictionLevel')]" doc:name="var-MandatoryFieldRestrictionLevel"/>
                <validation:matches-regex value="#[flowVars.MandatoryFieldRestrictionLevel]"  doc:name="Validation-MandatoryFieldRestrictionLevel" regex="^\s*\d*\s*$"/>
                <set-variable variableName="MinimumSearchScore" value="#[xpath3('//MinimumSearchScore')]" doc:name="var-MinimumSearchScore"/>
                <validation:matches-regex value="#[flowVars.MinimumSearchScore]" regex="^[a-zA-Z]{0,14}+$" doc:name="Validation-MinimumSearchScore"/>