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


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!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
Are you using XSLT2 or XSLT1?
lunchboxbillAuthor Commented:
sorry, unfortunately, XSLT1
Geert BormansInformation 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:when test="substring(normalize-space(.), 100, 1) = ' '">
                <xsl:value-of select="$firstOneHundred"/>
                <xsl:call-template name="upToLastSpace">
                    <xsl:with-param name="str" select="$firstOneHundred"/>
    <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, ' ')"/>


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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


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
lunchboxbillAuthor Commented:
Thank you!
Geert BormansInformation ArchitectCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.