XSLT - split string separated with specific character

Viewed 74

I have a xml like this,

<doc>
    <para>A brief 23 spell$of hea#vy rain$forc^%ed an early+ lunch$98@with nine</para>
</doc>

I need to break the text string from each $ present in the text.

SO expected output should be,

<doc>
    <para>A brief 23 spell</para>
    <para>of hea#vy rain</para>
    <para>forc^%ed an early+ lunch</para>
    <para>98@with nine</para>
</doc>

Can anyone suggest to me how can I do this using XSLT 1.0?

1 Answers

Try this

<xsl:template match="para">
    <xsl:call-template name="spilit">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="spilit">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '$')">
            <para><xsl:value-of select="substring-before($text, '$')"/></para>
            <xsl:call-template name="spilit">
                <xsl:with-param name="text" select="substring-after($text, '$')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <para><xsl:value-of select="$text"/></para>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

See Transformation at https://xsltfiddle.liberty-development.net/bEJbVrg

Related