DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Validate JSON Request Against JSON Schema in Mule 4
  • Loading XML into MongoDB
  • Exploring JSON Schema for Form Validation in Web Components
  • Datafaker Gen: Leveraging BigQuery Sink on Google Cloud Platform

Trending

  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Creating a Web Project: Caching for Performance Optimization
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • Code Reviews: Building an AI-Powered GitHub Integration
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Validate XML Request Against XML Schema in Mule 4

Validate XML Request Against XML Schema in Mule 4

In this article, we are going to configure an XML module in a Mule application to validate XML payloads using XML schema (XSD) and handle any errors.

By 
Ashish Jha user avatar
Ashish Jha
·
Aug. 22, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
10.6K Views

Join the DZone community and get the full member experience.

Join For Free

In the world of integration and data exchange, XML (eXtensible Markup Language) continues to play a crucial role due to its flexibility and widespread adoption. However, to ensure seamless communication between different systems, validating XML data against predefined rules becomes essential. In this article, we will explore the process of validating XML requests against XML Schema in Mule 4.

The steps involved in validating  XML schema in Mule 4 are as follows:

  1. Create a Mule project
  2. Define an XML schema
  3. Add schema to the project
  4. Add and configure the XML validation module
  5. Perform data validation
  6. Handle validation results

Before We Start, Let’s Understand What XML Schema Is

XML schema, also known as XSD (XML Schema Definition), is written in XML format and includes elements and attributes that describe the structure of the XML data. It serves as a set of rules or constraints defining the allowed elements, attributes, and relationships in an XML document. 

XML schema is used to ensure data consistency and validation in XML-based systems. XML Schema provides a standardized way to define the structure of XML documents, making it easier to share and understand the data requirements across different applications.

Use Case: XML Schema Validation:

XML Payload

XML
 
<root>
<success>true</success>
<message>XML schema validated</message>
<number>1234</number>
</root>


For this tutorial, we will pass the above payload with a POST request to the /xml endpoint and validate it using an XML schema. 

I have created a demo project with /json endpoint and /xml endpoint for JSON and XML validation, respectively. In this tutorial, we will use the /xml endpoint.

demo validation

For XML validation, we will use xml-validation-flow with /xml endpoint. Before configuring the validation module, if we make a call to /xml endpoint with the above XML payload, it gives a 200 status code with a success message.
XML validation

Steps To Validate the Payload Against XML Schema:

Step 1: Add the XML module to the project in the studio from Anypoint Exchange.

add dependencies

Verify whether the XML module has been added or not.

Verify whether the XML module has been added or not.

Step 2: Prepare the XML schema. For creating the XML schema, you can use any free online XML to XML schema generator tool available. (For this tutorial, I have used this one).

Select the XML to XSD from the left side menu.

Select the XML to XSD from the left side menu.

In the schema generator, add the payload and generate the schema.

In the schema generator, add the payload and generate the schema.

Generated Schema for the above XML payload:

XML
 
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="success" type="xs:boolean" />
        <xs:element name="message" type="xs:string" />
        <xs:element name="number" type="xs:unsignedShort" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>


Step 3: In the project's "src/main/resources" folder, create a folder with the name "schemas" (or any name you prefer). Create a file with the name "xml-schema.xsd" inside the "schemas" folder.

Note: Make sure that the file type of the XML schema is ".xsd."

Step 4: Copy and paste the generated XML schema to the "xml-schema.xsd" file.

Copy and paste the generated XML schema to the "xml-schema.xsd" file.

Step 5: Add the XML "Validate schema" processor before the "Transform Message" (or any other processor which needs the validated XML data).

Add the XML "Validate schema" processor before the "Transform Message" (or any other processor which needs the validated XML data).

Configure the XML schema path in the "Validate schema" processor.

Configure the XML schema path in the "Validate schema" processor.

Step 6: In the "Error handling" section, add an "On Error Propagate" scope and set the type to "XML-MODULE:SCHEMA_NOT_HONOURED" error type.

In the "Error handling" section, add an "On Error Propagate" scope and set the type to "XML-MODULE:SCHEMA_NOT_HONOURED" error type.

Step 7: Add the "httpStatus" variable and error message in the error handler. (In this tutorial, I have set the "httpStatus" to 400 for error response).

Step 8: Set the httpStatus variable in HTTP Listener as "Status code" in the "Responses" section.

Step 9: Deploy the project and send a valid request to the/xml endpoint. We should get a successful response with a status code of 200.

Deploy the project and send a valid request to the/xml endpoint. We should get a successful response with a status code of 200.

Now, make some changes to the request payload. We should get an error response with a status code of 400. (Here, I changed the "message" field to "messages").

Now, make some changes to the request payload.

Step 10: At this point, if we pass any extra fields in the XML request body, it will not allow the extra field to pass and will return an error message with 400 as the status code.

At this point, if we pass any extra fields in the XML request body, it will not allow the extra field to pass and will return an error message with 400 as the status code.

Note: In the above screenshot, the "id" field is extra, we did not define the "id" field in the XML schema, and that's why the request failed.

If we want to allow the extra fields that are not defined in the schema, we have to add "<xs:any processContents="lax" minOccurs="0" />" to the schema.

If we want to allow the extra fields that are not defined in the schema, we have to add "<xs:any processContents="lax" minOccurs="0" ></xs:any>" to the schema.

Step 11: Save the schema and redeploy the app. Now make a call to the /xml endpoint with extra fields in the request payload. It should allow the extra field to pass and should return a successful response.

Save the schema and redeploy the app.

This way, we can validate an XML request payload against an XML schema.

For more information about the XML schema properties, please refer to the documentation here.

You can get the source code of the above application here.

I hope you find this tutorial helpful.

JSON MULE XML Requests Schema

Opinions expressed by DZone contributors are their own.

Related

  • Validate JSON Request Against JSON Schema in Mule 4
  • Loading XML into MongoDB
  • Exploring JSON Schema for Form Validation in Web Components
  • Datafaker Gen: Leveraging BigQuery Sink on Google Cloud Platform

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!