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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

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

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

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

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Loading XML into MongoDB

Trending

  • Driving DevOps With Smart, Scalable Testing
  • How Kubernetes Cluster Sizing Affects Performance and Cost Efficiency in Cloud Deployments
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  1. DZone
  2. Coding
  3. Frameworks
  4. Transforming XML Messages Using XSLT With Apache Camel

Transforming XML Messages Using XSLT With Apache Camel

This tutorial will show you the method to transform XML messages with Extensible Stylesheet Language Transformations in Apache Camel.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Jul. 01, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
27.0K Views

Join the DZone community and get the full member experience.

Join For Free

1.0 Overview

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents or or other formats such as HTML for web pages, plain text or XSL Formatting Objects. The :xslt component in Apache Camel allows you to process message using XSLT template.

2.0 XSLT URI Format With Apache Camel

URI Description

xslt:com/example/mytransform.xsl

refers to file com/example/mytransform.xsl on the class path.

xslt:file:///example/mytransform.xsl

refers to file /example/mytransform.xsl

xslt:http://www.exampledemo.com/test/mytransform.xsl

refers to remote http resource.

3.0 Transforming an XML File to Another XML File Using XSLT

We will apply XSLT to transform an XML message to another XML message.

3.1 Input XML

<?xml version="1.0"?>
<rentalProperties>
    <property contact ="1">
        <type>House </type>
        <price>420</price>
        <address>
            <streetNo>1</streetNo>
            <street>Wavell Street</street>
            <suburb>Box Hill</suburb>
            <state>VIC</state>
            <zipcode>3128</zipcode> 
        </address>
        <numberOfBedrooms>3</numberOfBedrooms>
        <numberOfBathrooms>1</numberOfBathrooms> 
        <garage>1</garage>   
    </property>

3.2 XSLT

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" type="text/css" href="style.css">
    <xsl:template match="/">
        <rentalProperties>
            <property>
                <xsl:attribute name="contact"><xsl:value-of select='@contact'/></xsl:attribute>    
                <type><xsl:value-of select="type"/></type>
                <price><xsl:value-of select="price"/></price>
                <numberOfBedrooms><xsl:value-of select="numberOfBedrooms"/></numberOfBedrooms>
                <numberOfBathrooms><xsl:value-of select="numberOfBathrooms"/></numberOfBathrooms>
                <garage><xsl:value-of select="garage"/></garage>    
            </property>    
        </rentalProperties>    
    </xsl:template>
</xsl:stylesheet>

3.3 Output XML

The below output will be generated after applying XSLT to input XML.

<rentalProperties>
   <property contact="1">
      <type>House </type>
      <price>420</price>
      <address>1 Wavell Street,Box Hill,VIC, Australia</address>
      <numberOfBedrooms>3</numberOfBedrooms>
      <numberOfBathrooms>1</numberOfBathrooms>
      <garage>1</garage>
   </property>
</rentalProperties>

4.0 Transform an XML Message Using XSLT With Apache Camel

Please go through the article "Introduction to Apache Camel" and it will give you a basic idea how to create a Java project and add the Camel dependencies required to implement the Camel integration. 

package com.camel.router;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelXSLTTransform {
public static void main(String[] args) throws Exception
{
System.out.println("Initializing the camel context...");
CamelContext camelContext = new DefaultCamelContext();
System.out.println("Implementing Routes......");
camelContext.addRoutes(new RouteBuilder(){
@Override
public void configure() throws Exception{
from("file:F:\\Apache_Camel_Test\\IN")
.to("xslt:CamelFileRouterApp/src/mytransform.xslt")
.to("file:F:\\Apache_Camel_Test\\IN");
}
});
System.out.println("Starting Camel Context......");
camelContext.start();
      Thread.sleep(3000);
      camelContext.stop();
}
}

In the above example, we have used a URI template that refers XSLT file on the class path. Make sure your XSLT file is added to the class path. 

In this example, first it will read the file from the input directory and it will apply the transform and saved the transformed file to the output directory.

Image title

5.0 Testing

To test your application, drop the input XML to the input directory, and finally, run the application as Java. It will pick the file from the input directory, apply the XSLT transform, and save the output XML to the output directory.

6.0 Conclusion

Apache Camel provides very simple steps to apply XSLT transformations on your input XML. There are various mechanisms by which you can call XSLT into your Camel application, as described above.

Now you know how to transform XML messages using XSLT with Apache Camel.

XSLT XML Apache Camel

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Loading XML into MongoDB

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!