Curing the worst XSLT verbosity

Sunday, 8 Oct 2006 [Friday, 20 Oct 2006]

I was just hacking away at some appallingly verbose XSLT and thought how much easier on the eyes and wrists it would be if xsl:call-template could be avoided, which is by far the most explosive source of superfluous verbiage in transforms. Then it occurred to me that it’s entirely unnecessary: xsl:copy-of could perform the same function if XPath had a way of calling named templates. Armed with such a function, you could say the following:

<xsl:copy-of select="call-template( 'person-look-up', author[0] )/honorary-title"/>

Compare with the gyrations that are currently necessary:

<xsl:variable name="tmp">
  <xsl:call-template name="person-look-up">
    <xsl:with-param name="record" select="author[0]"/>
  </xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$tmp/honorary-title"/>

It’s just comical. I love XSLT as a concept, but the only reason I can stomach the actual syntax is that much of the heavy lifting in transforms happens in XPath expressions – and that is an elegantly concise language. It stands to reason that putting in more XPath can only help XSLT’s case…

I wonder how hard it would be to implement that as an extension function for libxslt.

Uche Ogbuji tells me to use EXSLT’s func:function instead. D’oh. Good point.