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

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

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them
  • Process Mining Key Elements

Trending

  • Top 5 Trends in Big Data Quality and Governance in 2025
  • When Caches Collide: Solving Race Conditions in Fare Updates
  • Are Traditional Data Warehouses Being Devoured by Agentic AI?
  • Building an AI Nutrition Coach With OpenAI, Gradio, and gTTS

XSLT Mediator Tutorial

By 
Madhuka  Udantha user avatar
Madhuka Udantha
·
May. 23, 13 · Interview
Likes (1)
Comment
Save
Tweet
Share
10.9K Views

Join the DZone community and get the full member experience.

Join For Free

This post will illustrate WSO2 ESB XSLT Transform Mediator with sample code.

When we need XSLT Mediator is need in wso2 ESB?

When we have to deal with a dynamic request for an ESB proxy. (If the proxy request is dynamic (not static/not predefined) we will have to use the XSLT mediator to mediate the message)

What is XSLT Mediator ?

The XSLT Mediator applies a specified XSLT transformation to a selected element of the current message payload. The source attribute specifies which element to have XSLT transformation. The feature element defines in to TransformerFactory.

If source element is not specified then soap body as the selected element. Parameters can be passed into the transformations through the property elements and it is optional and those can be accessed during transformation by <xsl:param name="the name of the property"/>.

What is the WSO2 ESB XSLT Mediator Syntax?

<xslt key="string" [source="xpath"]>
     <property name="string" (value="literal" | expression="xpath")/>*
     <feature name="string" value="true| false" />*
     <resource location="string" key="string"/>*
</xslt>
  • Source - Specifies which element to be selected to apply the given XSLT transformation.
  • Property - Allows optional parameters to be passed into the transformations.
  • Feature - Defines any features which should be explicitly set to the TransformerFactory.
  • Resource - Resolves XSLT imports and includes from the repository.

Here are some thing that I will be using in XSLT script

The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.[1]

omit-xml-declaration 

values : yes OR no  Optional.

"yes" specifies that the XML declaration (<?xml...?>) should be omitted in the output. "no" specifies that the XML declaration should be included in the output. The default is "no"

indent 

values:  yes OR no  Optional.

"yes" indicates that the output should be indented according to its hierarchic structure. "no" indicates that the output should not be indented according to its hierarchic structure.
This attribute is not supported by Netscape 6[2]

Let do a sample

We can try our Transport Services (BusServices)[3]

Here Is the Expecting request

<sample:getBusNo xmlns:sample="http://transport.org">
      <xs:rootId xmlns:xs="http://transport.org">2</xs:rootId>
   </sample:getBusNo>

Back end Serive expecting request

<p:getBusNo xmlns:p="http://transport.org">
<xs:rootId xmlns:xs="http://transport.org">2</xs:rootId>
</p:getBusNo>

1. Start BusServices in WSO2 AS
2. Start WSO2 ESB in offset 1
3. Go to Home > Manage > Service Bus > Local Entries in ESB
4. Then add Add In-lined XML Entry
5. Five name as "in_xslt" and Value as below (transform.xsl file contains)

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
                xmlns:xs="http://transport.org"
                xmlns:sample="http://transport.org"
                exclude-result-prefixes="sample fn">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//sample:getBusNo"/>
    </xsl:template>

    <xsl:template match="sample:getBusNo">
<p:getBusNo xmlns:p="http://transport.org">
      <xs:rootId xmlns:xs="http://transport.org">
<xsl:value-of select="xs:rootId"/>
</xs:rootId>
   </p:getBusNo>

    </xsl:template>
</xsl:stylesheet>

image

Now I need to change my respond 

from:

<ns:getBusNoResponse xmlns:ns="http://transport.org">
   <ns:return>4Colombo</ns:return>
   <ns:return>Negombo</ns:return>
   <ns:return>Galle</ns:return>
</ns:getBusNoResponse>

to:

<ns:getBusNoResponse xmlns:ns="http://transport.org">
<ns:rootName>2Colombo</ns:rootName>
<ns:rootName>Negombo</ns:rootName>
<ns:rootName>Galle</ns:rootName>
</ns:getBusNoResponse>

Here is XSLT Script for that

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
                xmlns:ns="http://transport.org"
                exclude-result-prefixes="ns fn">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//ns:getBusNoResponse"/>
    </xsl:template>

    <xsl:template match="ns:getBusNoResponse">
<ns:getBusNoResponse xmlns:ns="http://transport.org">
 <xsl:for-each select="ns:return">
   <ns:rootName> <xsl:value-of select="."/> </ns:rootName>

</xsl:for-each>
</ns:getBusNoResponse>
    </xsl:template>
</xsl:stylesheet>


image

Add it to the out sequences in proxy

image

Here is Final testing

image

image

NOTE
If any case If I get empty String in RootID I am adding new RootName

<xsl:choose>
    <xsl:when test=".!= ''">
         <rootNmae><xsl:value-of select="."/></rootNmae>
       
    </xsl:when>
    <xsl:otherwise>
       <rootNmae>Root<xsl:text> </xsl:text>text</rootNmae>
    </xsl:otherwise>
</xsl:choose>


XSLT Mediator (software) Element

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them
  • Process Mining Key Elements

Partner Resources

×

Comments

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
  • [email protected]

Let's be friends: