Search code examples
xsltwso2esbwso2-esb

Change an xml node name in ESB


I want to change an xml node name in WSO2 ESB, ie. I have the following xml

<MessageStatus xmlns="foo.example.org">
<ErrorCode>$1</ErrorCode>
<Message>$2</Message>
</MessageStatus>

and I want it to be this

<ItemName xmlns="foo.example.org">
<ErrorCode>$1</ErrorCode>
<Message>$2</Message>
</ItemName>

With ItemNames as a property; I mean they would change dynamically. Is there any way that I do this changes using ESB Mediators?


Solution

  • Finally I did this using XSLT Mediator, My Mediator config is like this:

    <xslt xmlns="http://ws.apache.org/ns/synapse" key="conf:/users/UsersXSLT.xslt">
       <property xmlns:ns="http://org.apache.synapse/xsd" name="TagName" expression="concat(get-property('OperationName'),'Response')"/>
    </xslt>
    

    which I defined a property for it that I can use it in my XSLT transformation. My XSLT is:

    <?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="http://www.jdnasir.ac.ir/EMI/UserProxy/"
    exclude-result-prefixes="fn">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    
    <xsl:template match="/">
    <xsl:apply-templates />
    </xsl:template>
    <xsl:param name="TagName"/>
    <xsl:template match="MessageStatus">
    <xsl:element name="{$TagName}" xmlns="http://www.jdnasir.ac.ir/EMI/UserProxy/">
    <xsl:for-each select="/MessageStatus/*">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:for-each>
    </xsl:element>
    </xsl:template>
    </xsl:stylesheet>
    

    The tricky part of this xslt was the <xsl:element name="{$TagName}" part.

    I hope this would help others.