Skip to main content

Concat two xml values with XSLT



The use-case described in this blog-post,is there's an WSO2 ESB node setup to proxy an incoming message to a particular back-end endpoint. 
Before delivering the message to the back-end endpoint,from the ESB node itself,this incoming message need to processed and change its inside xml payload format.


For eg: Below is the incoming message


<?xml version="1.0" encoding="UTF-8"?>
<CinemaHall name="liberty">
<OwnerData>
<Name>John Smith</Name>
<openedDate>12/12/80</openedDate>
<quality>good</quality>
</OwnerData>
<CinemaHallData>
<rows>100</rows>
<seats>
<seat>50</seat>
<seat>60</seat>
</seats>
</CinemaHallData>
</CinemaHall>

This message need to be changed as  below;

<?xml version="1.0" encoding="UTF-8"?>
<CinemaHall name="liberty">
<OwnerData>
<Name>John Smith</Name>
<openedDate>12/12/80</openedDate>
<quality>good</quality>
</OwnerData>
<CinemaHallData>
<rows>100</rows>
<seats> 50,60 </seats>
</CinemaHallData>
</CinemaHall>



Note I have highlighted which part of the payload need to be changed. So how to do that change. Simply you can use XSLT[XSL Transformations] support here.XSL means XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.Simply to do a XSLT transformation from ESB,you can use the support of in-built XSLT mediator in it. Additionally you will a XSL stylesheet which contains the conversion source logic apart from incoming XML payload.

In the above example conversion,you'll see there are multiple XML elements called <seat> in the XML input payload and thus,you need to iterate through each <seat> element and get each value of it and concat those values with a ',' at each iteration.And specially appending of ',' need to avoid in last iterated <seat>  value.
Thus xsl sheet would contain,a for loop and if-else condition as the conversion source.
In XSLT world for loop => < xsl:for-each> &lt/xsl:for-each>   and if-else condition =>< xsl:choose>  < xsl:when> < /xsl:when> <xsl:choose> 

And the sample xsl sheet source for above conversion is as below;


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//CinemaHall/CinemaHallData/seats">
    <seats>
   <xsl:for-each select="//CinemaHall/CinemaHallData/seats/seat">
        <xsl:variable name="i" select="position()" />  
     <xsl:choose>
     <xsl:when test="count(//CinemaHall/CinemaHallData/seats/seat)-position() >=1">      
         <xsl:value-of select="concat(//CinemaHall/CinemaHallData/seats/seat[$i] , ',')"/>
      </xsl:when>
      <xsl:when test="count(//CinemaHall/CinemaHallData/seats/seat)-position() =0">      
        <xsl:value-of select="//CinemaHall/CinemaHallData/seats/seat[$i]"/>
      </xsl:when>
   </xsl:choose>
    </xsl:for-each> 
   </seats> 
    </xsl:template>

</xsl:stylesheet>


Note to test,my XSL sheet correctness,I used online provided XSLT tool support.





Comments

  1. Hi, nice description about Concating two xml values with XSLT .Thanks for your help..

    -Aparna
    Theosoft

    ReplyDelete
  2. I get very helpful and useful information from your site . It aslo helpful for newcomers , keep going . I am a gamer and I would like to suggest to you something whichis really interesting . You may Visit here

    ReplyDelete
  3. Hello,
    I read your article it is amassing. I get very helpful and useful information from your site . It aslo helpful for newcomers , keep going . I am a gamer and I would like to suggest to you something whichis really interesting
    visit here

    ReplyDelete

Post a Comment

Popular posts from this blog

Convert an InputStream to XML

For that we can use DocumentBuilder class in java. By using the method parse(InputStream) ; A new DOM Document object will return. InputStream input; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document dc= parser.parse(input); In the above code segment,by using the created Document object,the corresponding XML file for the inputStream can be accessed. References: http://www.w3schools.com/dom/dom_intro.asp http:// download.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/DocumentBuilder.html

CORS support from WSO2 API Manager 2.0.0

Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources  on a web page to be requested from another domain outside the domain from which the first restricted resource was served. For example, an HTML page of a web application served from http://domain-a.com makes an <img src >  request for a different domain as 'domain-b.com' to get an image via an API request.  For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts as in above example and only allows to make HTTP requests to its own domain. To avoid this limitation modern browsers have been used CORS standard to allow cross domain requests. Modern browsers use CORS in an API container - such as  XMLHttpRequest  or Fetch - to mitigate risks of cross-origin HTTP requests.Thing to  note is it's not only sufficient that the browsers handle client side of cross-origin sharing,but also the servers from which these resources getting need to handl

[WSO2 AM] APIStore User Signup as an approval process

In previous versions of WSO2 APIManager before 1.6.0, it was allowed any user who's accessible the running APIStore come and register to the app.But there will be requirement like,without allowing any user to signup by him/her self alone,first get an approve by a privileged user and then allow to complete app registration.Same requirement can be apply to application creation and subscription creation as well.To fulfill that,we have introduced workflow extension support for  WSO2 APIManager  and you can find the introductory post on this feature from my previous blog post on " workflow-extentions-with-wso2-am-160 " . From this blog-post,I'll explain how to achieve simple workflow integration with default shipped resources with  WSO2 APIManager 1.6.0 and WSO2 Business Process Server 3.1.0 with targeting "user-signup" process. Steps First download the WSO2 APIManager 1.6.0[AM] binary pack from product download page . Extract it and navigate to