slimming down your XSLT output

2008-04-23 @ 14:28#

one of the side-effects of using XSLT to handle web app output (XHTML, JSON, Atom, XML, etc.) is that, if you're not careful, your final output can be riddled with extra carriage returns, tabs, and line feeds. usually this happens when your source data contains these characters. but sometimes the XSLT document will insert them, too. i found a recent XSLT document i was using was outputing close to 2K of these unneeded chars!

there are two easy ways to slim down your XSLT output:

xsl:text element

any time you are generating simple text within you XSLT document, you should wrap this text in the xsl:text element. that will make sure no LF, CR, TB chars are sent out in the response.

normalize-space() function

any time you suspect your source data contains extraneous CR, LF, and TB chars, you can use the normalize-space() function to remove them.

code