Re: [xsl] Use predicates/patterns or xsl:choose (was:] Stylesheet optimisation)

Subject: Re: [xsl] Use predicates/patterns or xsl:choose (was:] Stylesheet optimisation)
From: Geert Josten <Geert.Josten@xxxxxxxxxxx>
Date: Tue, 11 Jan 2005 16:55:40 +0100
Hi,

I was reading up all stuff about optimizing the performance of xslt. One basic advice I found is:

"Avoid complex patterns in template rules. Instead, use <xsl:choose> within the rule."

(Mike Kay at http://www.dpawson.co.uk/xsl/sect4/N9883.html#d12447e254)

This seems quite old (before July 2000), and I wondered if it is still true with newer Saxon or Xalan versions.

Michael can probably give the best answer, but I think that it still applies.


A parser has to test a node against all available templates and unless wrong ones fail very quickly (or unless some smart optimisation is done), evaluating all match patterns can consume considerable time. Using a choose within a single template for a particular element or node, is more or less like using a hashing mechanism to quickly cut out the templates that are not of interest.

This is a rather general thought, so I don't think it is likely to become out of date.

I rather split complex templates in more than one and adjust the match patterns accodingly to
shorten and simplify the actual code inside the xsl:template. So I try to avoid using xsl:choose
or xsl:if areas as much as possible as they make the stylesheet sometimes quite difficult to read
and maintain. And I was under the impression that splitting complex templates in easier ones by
making the match as precise as possible would also be quicker as I (naively) thought the
processor does something like a lookup table before actually performing the transformation.

I sometimes split complex templates into multiple ones as well, but usually only in combination with a mode on the template. Having lots of templates in the same scope (mode), isn't usually easier to maintain.


I'll leave the question on the lookup table for Michael or someone else..

> So, is it still better to avoid complex patterns or am I understanding the advice in the wrong
> way?

Ever thought about doing something like:

<xsl:template match="my-elem">
  <xsl:choose>
    <xsl:when test="..condition 1..">
      <xsl:apply-templates select="." mode="condition1" />
    </xsl:when>
    ...
  </xsl:choose>
</xsl:template>

<xsl:template match="my-elem" mode="condition1">
  .. handling of my-elem in condition 1 ..
</xsl:template>

Thanks a lot Chris

Cheers, Geert

Current Thread