Skip to content

Instantly share code, notes, and snippets.

Last active January 17, 2016 16:01
What would you like to do?
Here is how I am wrapping text via XSLT as opposed to CSS and JavaScript. The reason why I am prefering to do this
server side via XSLT as opposed to JavaScript client-side is because if I will be doing client-side JavaScripts,
I would have to escape the stuff during the HTML generation anyway in XSLT server-side. So in the same processing
power I might as well wrap it instead. Of course you can also do it via some fancy JavaScript client side by going
through the entire table and chopping up the text in each row, but that would make things look rather interesting
to the user (tables automatically re-arranging themselves).
Anyway, here is a simple recursive template based on the code posted by Andrew Welch:
<xsl:template name="text_wrapper">
<xsl:param name="text"/>
<xsl:param name="width" select="20"/>
<xsl:if test="string-length($text)">
<xsl:value-of select="substring($text, 1, $width)"/><br/>
<xsl:call-template name="text_wrapper">
<xsl:with-param name="text"
select="substring($text, $width + 1)"/>
<xsl:with-param name="width" select="$width"/>
<!-- To use it, call it as follows: -->
<xsl:call-template name="text_wrapper">
<xsl:with-param name="text" select="sometext"/>
<xsl:with-param name="width" select="20"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment