Umesh Perl... Umesh Perl... - 1 year ago 70
CSS Question

XSLT 1.0 Given String break a given position if the position is space otherwise before that word

I am new to XSLT, I read some stuff on xslt. I could not able to achieve the solution.

My input is like this.

<?xml version="1.0"?>
<line1>Hello world, XSLT is Functional programming? additional info</line1>

Required output

<line1>Hello world, XSLT is<br/>Functional programming?<br/>additional info</line1>

My conditions is
total size of the
<DIV width="20pt">
for that I need to break the string according to max size of 20 characters IF String position is 20 contains space break and continue to next, Otherwise if String position is 19 contains space break with 19 position.

Sorry for the very bad english.

Let me know this can be done in XSLT, Here variables are immutable. I can't append the values.

Any hints plz......


Answer Source

In XSLT 1.0 you could use a recursive template to split the string by checking if there is a space in the 20th or 21st position. (Or if the length is less than 20 in the first place)

Try this XSLT

<xsl:stylesheet xmlns:xsl="" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="text()[normalize-space()]" priority="2">
        <xsl:call-template name="split" />

    <xsl:template name="split">
        <xsl:param name="text" select="."/>
        <xsl:param name="limit" select="20" />
            <!-- Text length is less than or equal 20 characters -->
            <xsl:when test="string-length($text) &lt;= $limit">
                <xsl:value-of select="$text" />
            <!-- 20th or 21st character is a space -->
            <xsl:when test="contains(substring($text, $limit, 2), ' ')">
               <xsl:value-of select="substring($text, 1, $limit)" /> 
               <br />
               <xsl:call-template name="split">
                   <xsl:with-param name="text" select="normalize-space(substring($text, $limit + 1))" />
            <!-- Find first space after 20th character and split on that -->
               <xsl:value-of select="substring($text, 1, $limit)" />  
               <xsl:value-of select="substring-before(substring(concat($text, ' '), $limit + 1), ' ')" /> 
               <br />
               <xsl:call-template name="split">
                   <xsl:with-param name="text" select="substring-after(substring($text, $limit + 1), ' ')" />

    <xsl:template match="@*|node()">
            <xsl:apply-templates select="@*|node()"/>