Monday, March 10, 2014

Retrieve list of string as an array from a String

This process will accept two parameter as input

Input - This will be the input string that will be separated with some token values e.g 123:234:345:456

Token - This will be the token value based on which the process will get the content from the input and separate it to an array of values e.g. ":"

So for this set of input you will have following output



123
234
345
456

Now lets try to build this project in soa and see how this works.

The XSLT below is the crux of this transformation

<xsl:template match="/">
<client:processResponse>

<xsl:call-template name="Get-string">
<xsl:with-param name="Input" select="/client:process/client:input"/>
<xsl:with-param name="Token" select="/client:process/client:token"/>
</xsl:call-template>

</client:processResponse>
</xsl:template>
<!-- User Defined Templates -->
<xsl:template name="Get-string">
<xsl:param name="Input"/>
<xsl:param name="Token"/>
<xsl:variable name="newString">
<xsl:choose>
<xsl:when test="contains($Input $Token)">
<xsl:value-of select="normalize-space($Input)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(normalize-space($Input), $Token)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="result" select="substring-before($newString, $Token)"/>
<xsl:variable name="TestString" select="substring-after($newString, $Token)"/>
<xsl:variable name="count" select="position()"/>
<client:result>
<xsl:value-of select="$result"/>
</client:result>
<xsl:if test="$TestString">
<xsl:call-template name="Get-string">
<xsl:with-param name="Input" select="$TestString"/>
<xsl:with-param name="Token">
<xsl:value-of select="$Token"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>


As you can see it takes two parameter and then get the list of string from input parameter based on the token provided.

Once you will develop the code you will have following input parameter




Test with any dummy parameters and the token value which you want to use for separation

the result for the above input is as shown below



There are only few things that you need to take care that is how the output response should appear

Hence in the code i have used this piece of code to get the result in the output string

<client:result>
<xsl:value-of select="$result"/>
</client:result>


And as you can observer this output node was not defined in the call to the template

<xsl:template match="/">
<client:processResponse>

<xsl:call-template name="Get-string">
<xsl:with-param name="Input" select="/client:process/client:input"/>
<xsl:with-param name="Token" select="/client:process/client:token"/>
</xsl:call-template>

</client:processResponse>
</xsl:template>

So you can manipulate your code accordingly to get desired result.

No comments: