FAQ - General

Question
Use XSLT to truncate a string depending on it's length
Answer

Sometimes we need to truncate a string if it is longer than a particular number of characters - for example when putting an entry into a navigation bar or other application where there is limited space.

Umbraco is very XSLT orientated and our navigation menu uses it to dynamically generate it's items. We found the need to truncate the titles of these items to keep the menu looking tidy. Here's how:

 

 

<xsl:choose>

 

<!-- if YOUR_STRING is greater than the MAXIMUM_LENGTH -->
<xsl:when test="string-length( YOUR_STRING ) &gt; MAXIMUM_LENGTH ">

 

<!-- print out the truncated value followed by "..." -->
<xsl:value-of select="substring( YOUR_STRING ,0, MAXIMUM_LENGTH )"/>...

</xsl:when>

<xsl:otherwise>

 

<!-- otherwise print out the whole, un-truncated string -->

<xsl:value-of select=" YOUR_STRING "/>

</xsl:otherwise>

</xsl:choose>

 

 

Remember to replace YOUR_STRING with your string's name and MAXIMUM_LENGTH with a whole number, e.g. 8 or 113

If you found this information useful, please share it!


This FAQ was last updated on Tuesday, January 07, 2014

Contact Information

To find out more about Ava solutions you can contact us in a number of ways:
Follow Us...