Archive for August, 2008

The Wilson-Sidlinger Method

August 16th, 2008  |  Published in development

Philip Carl was working on some XSL yesterday and noticed a fair amount that looked like this:

<table>
  <col…/>
  <col…/>
  <col…/>
  <x:apply-templates select="…" />
</table>

It was everywhere, and we all know to not repeat ourselves. But neither of us knew at the time how to wrap the execution of an XSL template in some boilerplate. Ladies and gentlemen, may I present the Wilson-Sidlinger Method†:

<x:template name="wrapWithTable">
  <x:param name="innards" />
  <table>
    <col…/>
    <col…/>
    <col…/>
    <x:copy-of select="$innards" />
  </table>
</x:template>

<x:template match="critter"> … <x:call-template name="wrapWithTable"> <x:with-param name="innards"> <x:apply-templates select="kin" /> </x:with-param> </x:call-template> … </x:template>

<x:template match="you_uns"> … <x:call-template name="wrapWithTable"> <x:with-param name="innards"> <x:apply-templates select="mama" /> <x:apply-templates select="em" /> </x:with-param> </x:call-template> … </x:template>

We verified that execution time was not greatly affected. As a matter of fact, loading the stylesheet was a little faster with the reduced amount of code.

Is the apply-template being passed to the called template as a function or the result of a function? We may never know. The great thing about XSL, though, is it is a side-effect-free language that lets us care not one bit.

† This method of dealing with XSL repetition may have been used before, but we couldn’t find any mention of it.

Update: P. Carl corrected me. It’s copy-of in the “wrapWithTable” template.