2
votes

I have a string like

*this is text1 * this is text2 *this is text3

I want the output by spliting the text using * in my pdf like

this is text1 
this is text2 
this is text3

my text coming in @value of xslt like

     <fo:block linefeed-treatment="preserve" > 
         <xsl:value-of select="@key"/>
             </fo:block>
                <fo:block linefeed-treatment="preserve" > 
              <xsl:value-of select="@value"/>
                </fo:block>
            <fo:block linefeed-treatment="preserve" > 
                 <xsl:text>&#xA;</xsl:text>
             </fo:block>
    </fo:block>

How can I split the string produce the output. Please suggest. I'm using xsl 1.0

1
possible duplicate of split function in xslt 1.0Erwin Bolwidt
The simplest solution is using XSLT 2.0, with the tokenize() function. If you can't use XSLT 2.0, then you will either need to write a template yourself (which might not be trivial as you are new to XSLT) or use extensions such as EXSLT. In the linked question you have examples of all those alternatives: EXSLT (solution using node-set and links to examples with tokenize), XSLT 2.0 and even a full named template. If they are confusing, you should read a bit about named templates and extensions.helderdarocha

1 Answers

4
votes

First call a template which performs the splitting for you instead of the value-of:

<xsl:call-template name="split">
    <xsl:with-param name="text" select="@value"/>
</xsl:call-template>

Here is the named template:

<xsl:template name="split">
    <xsl:param name="text" select="."/>

    <xsl:if test="string-length($text) > 0">
        <xsl:variable name="output-text">
            <xsl:value-of select="normalize-space(substring-before(concat($text, '*'), '*'))"/>
        </xsl:variable>

        <xsl:if test="normalize-space($output-text) != ''">
            <xsl:value-of select="$output-text"/>
            <xsl:text>&#xA;</xsl:text>
        </xsl:if>

        <xsl:call-template name="split">
            <xsl:with-param name="text" select="substring-after($text, '*')"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

Input (value of the @value):

*this is text1 * this is text2 *this is text3

Output:

this is text1
this is text2
this is text3