How to Split a String using/in XSL.
This example will show how to split a string into a node set using delimiter in XSL.
Example:
Input:
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/SplitString file:/D:/JDevMyWork/BPMApplication/SplitString/xsd/SplitString.xsd" xmlns="http://xmlns.oracle.com/SplitString">
<CheckList>Bank statement, License, ID proof</CheckList>
</Test>
<CheckList>Bank statement, License, ID proof</CheckList>
</Test>
Output:
<ns0:Test xmlns:ns0="http://xmlns.oracle.com/SplitString" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/SplitString file:/D:/JDevMyWork/BPMApplication/SplitString/xsd/SplitString.xsd">
<ns0:CheckList>Bank statment</ns0:CheckList>
<ns0:CheckList>Bank statment</ns0:CheckList>
<ns0:CheckList>License</ns0:CheckList>
<ns0:CheckList>ID Proof</ns0:CheckList>
</ns0:Test>
</ns0:Test>
In this example I have used same xsd for both input and output. You can use different type of variables too. As you can see based on "," as delimiter the input string has been divided into a node-set.
You will need to use Recursive templates to achieve this as shown below.
Complete XSL:
<xsl:template match="/">
<ns0:Test>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="/ns0:Test/ns0:CheckList"/>
</xsl:call-template>
</ns0:Test>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="text" select="$text"/>
<xsl:param name="separator" select="','"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<ns0:CheckList>
<xsl:value-of select="normalize-space($text)"/>
</ns0:CheckList>
</xsl:when>
<xsl:otherwise>
<ns0:CheckList>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</ns0:CheckList>
<xsl:call-template name="tokenize">
<xsl:with-param name="text"
select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<ns0:Test>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="/ns0:Test/ns0:CheckList"/>
</xsl:call-template>
</ns0:Test>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="text" select="$text"/>
<xsl:param name="separator" select="','"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<ns0:CheckList>
<xsl:value-of select="normalize-space($text)"/>
</ns0:CheckList>
</xsl:when>
<xsl:otherwise>
<ns0:CheckList>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</ns0:CheckList>
<xsl:call-template name="tokenize">
<xsl:with-param name="text"
select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sample project can be downloaded from here: https://drive.google.com/folderview?id=0B-B6k-1tHlm4eHpyWlhmdjJaWUU&usp=sharing
Shoot a comment, if you need any help.
No comments:
Post a Comment