XML Module:

The XML Module can process and extract data from an XML document. Although DataWeave is recommended for most XML-related use cases, the XML module should be used for cases that involve the use of XML standards such as XSLT, XPath and XQuery, or XSD.
To use the XML module, you simply add it to your Mule app through the Studio or Flow Designer UI, or you can add the following dependency in your pom.xml file: Learn more from Mulesoft Online Training
<dependency>
<groupId>org.mule.modules</groupId>
<artifactId>mule-xml-module</artifactId>
<version>1.1.0</version> <!– or newer –>
<classifier>mule-plugin</classifier>
</dependency>
Validate Against a Schema
When using the XML Module to validate against a schema that has references to other local schema files, validation can fail because the access was restricted by the expandEntities as it was using the default value of NEVER. The error message is: The supplied schemas were not valid. schema_reference: Failed to read schema document NMVS_Composite_Types.xsd, because file access is not allowed due to restriction set by the accessExternalSchema property.
You can eliminate this issue by adding expandEntities="INTERNAL" to the xml-module:config element.
Validating Documents against an XSD Schema with the XML Module
The <xml-module:validate-schema> operation validates that the input content is compliant with a given schema. This operation supports referencing many schemas (using comma as a separator) that include each other.
This example shows how to work with XML that contains the script of the play Othello by William Shakespeare. The XML file looks something like this:
<flow name=”process”>
<xml-module:validate-schema schemas=”schema1.xsd, schema2.xsd” />
<flow-ref name=”processValidDocument” />
</flow>
By default, this operation looks for the input document at the message payload level. However, you can supply your own input, for example:
<flow name=”process”>
<file:read path=”document.xml” target=”xmlDoc” />
<xml-module:validate-schema schemas=”schema1.xsd, schema2.xsd”>
<xml-module:content>#[vars.xmlDoc]</xml-module:content>
</xml:module:validate-schema>
<flow-ref name=”processValidDocument” />
</flow>
Get more in-depth knowledge, enroll for live demo on Mulesoft Training
Note that you can use the <xml-module:validate-schema> component inside a <validation:all> element.
Handling the Validation Error
If the validation is successful, nothing happens, and the flow continues to the next operation. If it fails, an XML-MODULE:SCHEMA_NOT_HONOURED error is raised.
The XML-MODULE:SCHEMA_NOT_HONOURED error is a complex one. Because the validation can fail for any number of reasons, the error contains a list of Messages. Each message contains a SchemaViolation object, which has the following structure:
SchemaViolation Object
{
lineNumber: Number,
columnNumber: Number,
description: String
}
Consider the following example:
<flow name=”extractErrorsFromException”>
<try>
<xml-module:validate-schema schemas=”schema.xsd” /> (1)
<error-handler>
<on-error-propagate type=”XML-MODULE:SCHEMA_NOT_HONOURED”> (2)
<foreach collection=”#[error.errorMessage.payload]”>
<logger level=”ERROR” message=”#[‘At line: $(payload.lineNumber), column: $(payload.columnNumber) -> $(payload.description)’]” /> (3)
</foreach>
</on-error-propagate>
</error-handler>
</try>
</flow>
ERROR 2018-02-16 14:35:45,722 [[MuleRuntime].cpuIntensive.01: [SchemaValidationTestCase#extractErrorsUs
To get in-depth skills learn more with Mule Training with free demo