Friday, October 16, 2015

Adding namespace and prefix to tags inside payload in wso2 ESB using XSLT

In this post I will share how to add namespace and prefix to tags inside payload in wso2 ESB using XSLT Mediator


We are getting following soap request


  1. <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">  
  2.    <soapenv:Body>  
  3.     <catalog>       
  4.        <product>    
  5.           <number>100</number>    
  6.           <name>BaseBall</name>    
  7.           <colourChoices>Black</Currency>    
  8.           <price>$50</DateTime>  
  9.        </product> 
  10.       </ExchangeRate 
  11.     </catalog>  
  12.    <soapenv:Body>  
  13. <soapenv:Envelope>  


Then we want to add ex0 prefix to catalog tag and namespace must be http://sample.com/test//blog/Sample 


So we need to create sample.xslt style sheet like below


  1.   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">  
  2.     <xsl:output indent="yes"/>  
  3.     <xsl:strip-space elements="*"/>  
  4.     <!--match all the nodes and attributes-->  
  5.     <xsl:template match="node()|@*">  
  6.         <xsl:copy>  
  7.             <xsl:apply-templates select="node()|@*">  
  8.         </xsl:apply-templates></xsl:copy>  
  9.     </xsl:template>  
  10.     <!--Select the element need to be apply the namespace and prefix -->  
  11.     <xsl:template match="catalog">  
  12.         <!--Define the namespace with prefix ns0 -->  
  13.         <xsl:element name="ex0:{name()}" namespace="http://sample.com/test/blog/Sample">  
  14.             <!--apply to above selected node-->  
  15.             <xsl:apply-templates select="node()|@*">  
  16.         </xsl:apply-templates></xsl:element>  
  17.     </xsl:template>  
  18. </xsl:stylesheet>  



Then Go to Manage-> Service Bus-> Local Entries and add the created sample.xslt file.


Then in your proxy add xslt mediator as <xslt key="sample.xslt"/> and refer the sample.xslt. Add a log mediator after that to check the changed payload


  1. <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">  
  2.    <soapenv:Body>  
  3.       <ex0:catalog xmlns:ex0="http://sample.com/test/blog/Sample">  
  4.          <product>  
  5.             <number>100</number>  
  6.             <name>BaseBall</name>  
  7.             <colourChoices>Black</colourChoices>  
  8.             <price>50</price>  
  9.          </product>  
  10.       </ex0:catalog>  
  11.    </soapenv:Body>  
  12. </soapenv:Envelope>
 





No comments:

Post a Comment

Blogger Widgets