Validate Documents Against an XSD Schema with the XML Module - Mule 4
The XML module Validate schema operation validates that the input content is compliant with a given XSD schema. The operation supports referencing many schemas, using a comma (,
) as a separator.
By default, this operation looks for the input document at the message payload level, but you can supply your own input as well.
Note that you can use the Validate schema operation inside the Validation module All scope.
Configure the Validation Schema Operation in Studio
In the following example, you validate XML schemas that contain a script:
-
In Studio, drag the Validate schema operation to your flow. In the Schemas field, add your schema files, separating filenames by using a comma, for example,
schema1.xsd, schema2.xsd
. -
Drag a Flow Reference component to the right of the Validate schema operation.
-
Set Flow name to
processValidDocument
.
In the Configuration XML editor, the <xml-module:validate-schema>
configuration looks like this:
<flow name="process">
<xml-module:validate-schema schemas="schema1.xsd, schema2.xsd" />
<flow-ref name="processValidDocument" />
</flow>
In the following example, you configure the Validate schema operation by supplying your own input:
-
In Studio, drag the File Read operation to your flow.
-
In the configuration screen, set File Path to
document.xml
. -
In the Advanced tab, set Target Variable to
xmlDoc
. -
Drag the XML module Validate schema operation to the right of the Read operation.
-
In the Schemas field, add your schemas and separate their names by using commas, for example,
schema1.xsd, schema2.xsd
. -
Set the Content field to the XML content to validate, for example,
#[vars.xmlDoc]
. -
Drag a Flow Reference component to the right of the Validate schema operation.
-
Set Flow name to
processValidDocument
.
In the Configuration XML editor, the <xml-module:validate-schema>
and <xml-module:content>
configurations look like this:
<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>
Manage the Validation Error
If the validation is successful, the flow continues to the next operation. However, if the validation fails, an XML-MODULE:SCHEMA_NOT_HONOURED
error is raised.
Because the validation can fail for more than one reason, the error description contains a list of messages. Each message contains a SchemaViolation
object that has the following structure:
{
lineNumber: Number,
columnNumber: Number,
description: String
}
In the following example, you configure the Validate schema operation and the XML-MODULE:SCHEMA_NOT_HONOURED
error in the Error Handler component:
-
In Studio, drag the Try scope component to your flow.
-
Drag the XML module Validate schema operation into the Try scope component.
-
In the Schemas field, add your schema, for example,
schema.xsd
. -
In your flow, click the Error handling arrow to expand the error-handling section.
-
Drag the On Error Propagate component into the Error handling section.
-
Set the Type field to the Mule error handled by this on-error strategy, for example,
XML-MODULE:SCHEMA_NOT_HONOURED
. -
Deselect Enable Notifications and Log Exception fields.
-
Drag a For Each scope component into the On Error Propagate component to iterate over each
errorMessage.payload
. -
Set the Collection field to the expression that splits the payload into individual pieces, for example,
#[error.errorMessage.payload]
. -
Drag a Logger component into the For Each scope component to log the XML module error.
-
Set the Message field to the expression that contains the
SchemaViolation
objects that describe the message, for example,#['At line: $(payload.lineNumber), column: $(payload.columnNumber) → $(payload.description)']
. -
Save your changes.
-
Click the project name in Package Explorer and then click Run > Run As > Mule Application.
-
Navigate to the Console view to read the logger message:
ERROR 2018-02-16 14:35:45,722 [[MuleRuntime].cpuIntensive.01: [SchemaValidationTestCase#extractErrorsUsingExpressions].extractErrorsFromException.CPU_INTENSIVE @411e886b] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: At line: -1, column: -1 -> cvc-complex-type.2.4.a: Invalid content was found starting with element 'fail'. One of '{used}' is expected.
In the Configuration XML editor, the <on-error-propagate type="XML-MODULE:SCHEMA_NOT_HONOURED">
configuration looks like this:
<flow name="extractErrorsFromException">
<try>
<xml-module:validate-schema schemas="schema.xsd" />
<error-handler>
<on-error-propagate type="XML-MODULE:SCHEMA_NOT_HONOURED">
<foreach collection="#[error.errorMessage.payload]">
<logger level="ERROR" message="#['At line: $(payload.lineNumber), column: $(payload.columnNumber) -> $(payload.description)']" />
</foreach>
</on-error-propagate>
</error-handler>
</try>
</flow>