XSL - take first 100(ish) characters of a string, but split at the end of a word

Hi,

I have a string value, that is sometimes too long to display to the web page.

After about 100 charcters, I want to stop displaying it, but I dont want to stop displaying during a word - it must finish at the end of a complete word.

A space will define the end of a word.

An help will be much appreciated!
LVL 1
lunchboxbillAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
OK, you will need some recursion then

consider the long string in an element test

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="test">
        <xsl:variable name="firstOneHundred" select="substring(normalize-space(.), 1, 100)"/>
        <xsl:text>&#10;</xsl:text>
        <xsl:choose>
            <xsl:when test="substring(normalize-space(.), 100, 1) = ' '">
                <xsl:value-of select="$firstOneHundred"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="upToLastSpace">
                    <xsl:with-param name="str" select="$firstOneHundred"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
   
    <xsl:template name="upToLastSpace">
        <xsl:param name="str"/>
        <xsl:value-of select="substring-before($str, ' ')"/>
            <xsl:if test="contains(substring-after($str, ' '), ' ')">
                <xsl:text> </xsl:text>
                <xsl:call-template name="upToLastSpace">
                    <xsl:with-param name="str" select="substring-after($str, ' ')"/>
                </xsl:call-template>
            </xsl:if>
    </xsl:template>

</xsl:stylesheet>
0
 
Geert BormansInformation ArchitectCommented:
Are you using XSLT2 or XSLT1?
0
 
lunchboxbillAuthor Commented:
sorry, unfortunately, XSLT1
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Geert BormansInformation ArchitectCommented:
I first store the 100 first characters of the text in a variable, with normalize-space to make sure that every white-space seperator is a single space
If character 101 is a space? I use the variable (borderline condition of exactly 100 chars)
If not, I pass the string to a recursive template
I am not sure what happens when character 100 is a space, you might have to test that and add a condition if necessary

cheers


Geert
0
 
Geert BormansInformation ArchitectCommented:
By the way, this doesn' work if the first word is over 100 chars, but that is something you can easily test
0
 
lunchboxbillAuthor Commented:
Thank you!
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
All Courses

From novice to tech pro — start learning today.