MuleSoft
XML Schema Validation Using MuleSoft

XML Schema Validation Using MuleSoft

Pre-requisites

  • We need to have an Anypoint Studio should be setup.
  • Basic knowledge of Mule 4 and DataWeave.

XML Validate Schema

Overview

  • When working with XML in enterprise integrations, it’s important to ensure that incoming XML payloads follow a predefined structure.
  • MuleSoft provides built-in support for XML Schema (XSD) validation, which allows developers to enforce rules on the payload before it proceeds further in the flow.
  • This approach improves data quality and system reliability by catching errors early in the integration process.
  • In this blog, we demonstrate how to configure MuleSoft to validate XML payloads against an XSD.
  • The validation step ensures that required elements are present, data types are correct, and the structure matches the schema.
  • If the XML does not comply, Mule will raise a validation error, preventing invalid data from propagating through the system.

Steps:

  1. To begin, a new Mule project needs to be created. Then, a HTTP listener component dragged and dropped into the project from the HTTP module. Configure the listener, and set the host to All interface (0.0.0.0), port number as 8081. Set the path as /xmlSchema.

  1. Following the addition of the listener component, include two loggers to mark the beginning and the end of the flow. By logging the message β€œStart of flow” ++ (flow.name as String) in start-logger and β€œEnd of flow” ++ (flow.name as String) in end-logger, in expression mode.

XML Schema Definition:

<?xml version=“1.0” encoding=“UTF-8” ?>

<xs:schema xmlns:xs=“http://www.w3.org/2001/XMLSchema”>

<xs:element name=“shiporder”>

<xs:complexType>

<xs:sequence>

<xs:element name=“orderperson” type=“xs:string”/>

<xs:element name=“shipto”>

<xs:complexType>

<xs:sequence>

<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:sequence>

</xs:complexType>

</xs:element>

<xs:element name=“item” maxOccurs=“unbounded”>

<xs:complexType>

<xs:sequence>

<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:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

<xs:attribute name=“orderid” type=“xs:string” use=“required”/>

</xs:complexType>

</xs:element>

</xs:schema>

  1. Next, In the project’s src/main/resources directory, create a new folder named schemas, then inside it create a new file called mySchema.xsd, and paste the XML Schema definition into this file.

  2. Next, import the XML module from Mule Palette into your project. Next, drag and drop the Validate Schema component from the Validation module onto the canvas and configure it as demonstrated below.
    Schemas: schemas/mySchema.xsd
    Schema contents: None
    Schema language: W3C(Default)
    Content: payload

  3. Wrap the Validate Schema component inside a Try scope, then add an On Error Propagate within the error handling section. Inside the error block, place a Logger to capture and display the validation error message. This ensures that whenever an invalid XML payload is received, the flow gracefully handles the exception and returns a proper error response instead of breaking the execution.

XML payload:

<?xml version=”1.0″ encoding=”UTF-8″?>

<shiporder orderid=”889923″

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:noNamespaceSchemaLocation=”shiporder.xsd”>

<orderperson>John Smith</orderperson>

<shipto>

<name>Ola Nordmann</name>

<address>Langgt 23</address>

<city>4000 Stavanger</city>

<country>Norway</country>

</shipto>

<item>

<title>Empire Burlesque</title>

<note>Special Edition</note>

<quantity>1</quantity>

<price>10.90</price>

</item>

<item>

<title>Hide your heart</title>

<note>Special Edition</note>

<quantity>1</quantity>

<price>9.90</price>

</item>

</shiporder>

  1. Now deploy the Mule application locally and open Postman to test it. Make a POST request to your Mule application endpoint (http://localhost:8081/xmlSchema). In the request body, choose raw β†’ XML and paste the XML payload you want to validate. If the payload is valid according to the schema, the application will return a 200 OK response, confirming the XML is valid. If the payload does not match the schema, the flow will trigger the error handler and return a validation error response, indicating that the XML validation has failed..

  1. Next, modify the XML payload by removing or altering one of the required elements defined in the schema (for example, omit the <name> element). Send this updated payload in Postman to the same endpoint. Since the XML no longer matches the schema, the application will respond with a 500 Internal Server Error (validation failed), and the error handler will log the validation failure message. This confirms that the validation is working as expected.

πŸš€ Validate Before You Integrate

Don’t let invalid XML payloads disrupt your enterprise workflows. Proper XSD validation in MuleSoft ensures clean data, reliable integrations, and fewer production errors.

At TGH, we design secure, scalable MuleSoft solutions with advanced validation, error handling, and API-led architecture tailored for enterprise environments.

πŸ“ž +91 88106 10395
🌐 https://techygeekhub.com/contact-us

Connect with our MuleSoft experts today and build integration flows that are accurate, resilient, and future-ready.

Author

TGH Software Solutions Pvt. Ltd.

Leave a comment

Your email address will not be published. Required fields are marked *