Duplicates in XSLT
Hi
I am using the following code to remove the duplicates and also to count the number of duplicates...
<xsl:variable name="string" select="'Hello, Hello,
Hello, test, dog, cat, cat'" />
<xsl:template match="/">
<xsl:choose>
<xsl:when test="not(contains($string, ','))">
<xsl:value-of select="$string" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="remove-duplicates">
<xsl:with-param name="string" select="translate($string, ' ', '')" />
<xsl:with-param name="newstring" select="''" />
<xsl:with-param name="count" select="1" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="remove-duplicates">
<xsl:param name="string" />
<xsl:param name="newstring" />
<xsl:param name="count" />
<xsl:choose>
<xsl:when test="$string = ''">
<xsl:value-of select="$newstring" />
</xsl:when>
<xsl:otherwise>
<xsl:if test="contains($newstring,substring-before($string, ','))">
<xsl:call-template name="remove-duplicates">
<xsl:with-param name="string" select="substring-after($string, ',')" />
<xsl:with-param name="newstring" select="$newstring" />
<xsl:with-param name="count" select="1+$count" />
</xsl:call-template>
</xsl:if>
<xsl:if test="not(contains($newstring, substring-before($string, ',')))">
<xsl:variable name="temp">
<xsl:if test="$newstring = ''">
<xsl:value-of select="substring-before($string, ',')" />
<xsl:text>-</xsl:text>
<xsl:value-of select="$count" />
</xsl:if>
<xsl:if test="not($newstring = '')">
<xsl:value-of select="concat($newstring, ',', substring-before($string, ','))" />
<xsl:text>-</xsl:text>
<xsl:value-of select="$count" />
</xsl:if>
</xsl:variable>
<xsl:call-template name="remove-duplicates">
<xsl:with-param name="string" select="substring-after($string, ',')" />
<xsl:with-param name="newstring" select="$temp" />
<xsl:with-param name="count" select="1" />
</xsl:call-template>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
I am getting the following Output
Hello-1, test-3, dog-1, cat-1
But my desired output is
Hello-3, test-1, dog-1, cat-2
Can anybody help me to achieve this....
|