Monday, March 03, 2014

Replace activity in SOA

As of now there is no replace activity in SOA ,there is a translate function in BPEL but it just changes one character and can not be used to replace string contecnt in your payload. This can be achieved either through java embedding or through XSLT. In this exercise we will try to use the XSLT to do the replace.

The exact output will be something like this



As you can see this function has three input parameter

The first "text" is the input parameter or the string data from which we have to replace some content.

Replace is the string that needs to be substituted in the "text" data.

"with" is the input data which will actually replace the Replace string in the text data.

In order to achieve this create an XSLT file (Make sure you have the variable in target side where you wanted to store the result of replace)

Once you have the XSL created --> go to the source code and use the following code snippet


<client:process>
<client:input>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="/client:processResponse/client:result"/>
<xsl:with-param name="replace" select='"key1"'/>
<xsl:with-param name="with" select='"1234"'/>
</xsl:call-template>
</client:input>
</client:process>
</xsl:template> <!-- User Defined Templates --> <xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>


Here you have to make sure that the text is the actual xpath to the input parameter where in you wanted to perform the replace function.

Save the project and deploy to test it.

In this demo project we have input parameters coming from "/client:processResponse/client:result"

Key1 is the string that has to be searched from this input parameter and

1234 is the string that will replace the occurrence of "key1" in the input parameter.

To understand lets take input and try to understand the output

(text)"/client:processResponse/client:result" =The password is key1
Replace =key1
with=1234

Now if following parameters will be passed the result of this operation will be

The password is 1234

No comments: