Tue
18
Jul '06
XSL Best Practice
by Frank Spychalski filed under Rants, Work

I have to work with XSL templates again and in addition to XML being a terrible language to write any kind of code in I found this anti-pattern.

If you want to output a few attributes with little or not text in between, please refrain from


<xsl:value-of select="@type"/> <xsl:value-of select="@lfd"/> - <xsl:value-of select="@key"/>: <xsl:value-of select="@value"/>

and use concat instead

<xsl:value-of select="concat(@type, ' ', @lfd, ' - ', @key, ': ', @value, ' ')"/>

Why? Less code[1], easier to read, no problems with whitespaces in your result document.

For further reading I found XSLT Best practices fairly informative. Are there any other good compilations of XSL best practices?

[1] before anyone starts argueing that it’s not much of a difference, I just removed examples with 8 calls to xsl:value-of…


Any comments? Or questions? Just leave a Reply: