Re: [xsl] Efficiency, use param in key?

Subject: Re: [xsl] Efficiency, use param in key?
From: "Marrow" <marrow@xxxxxxxxxxxxxx>
Date: Sat, 3 Aug 2002 11:02:41 +0100
Hi Chris,

>1. is there anything I should do to improve it (make it more efficient)?

Possibly - you have a template match for the distinct <file_audio> elements (by
@fileref) e.g.
  <xsl:template match="file_audio[generate-id() =

But no template match for the other, non-distinct, <file_audio> elements?  Is
this by design or is that part of the stylesheet omitted?

If you don't have a template for the non-distinct <file_audio> elements then you
are relying on the built-in template rules to handle these (which, by chance,
will not output anything because the <file_audi> elements happen to have no
child elements).  But this would still mean that the non-distinct elements are
still being processed - and in your case have to be sorted and the
transformation engine has to hunt for a matching template.

Therefore, it might improve efficiency if you move the distinct filtering to the
<xsl:apply-templates>, e.g.

  <xsl:apply-templates select="//file_audio[generate-id() =
    <xsl:sort select="@fileref"/>

(although using //, as Dimitre has already pointed out, is usually a hit on
performance in itself - try to use the appropriate path where it is known).

>2. is it possible to use the param "ele" in key, apply-templates and

You can't use variables/params in match patterns.  Variables and params can only
be used in XPath expressions - and even though @use is an expression it can't
contain variable references either (this is possibly why the designers decided
to call it @use rather than @select?).

>This would make it possible for a single stylesheet to look for
>file_audio, file_video or file_graphic tags.

If efficiency is of primary concern then it is likely that a separate stylesheet
for each type will yield better performance than trying to parameterize the same
stylesheet to handle all.  But in either case (separate stylesheets or a single
parameterized stylesheet) you will still only be able to 'select distinct' on
known design time factors (i.e it is unlikely you will be able to parameterize
to the point where any possible element and distinct attribute might be passed).

But that is not to say your stylesheet cannot be parameterized to cope with the
task.  When using keys this can often be achieved by taking the part of the
<xsl:key>'s @match that is to be parameterized and moving it into the @use
clause as a static value (the static value can then be passed in when the key()
function is used), e.g.

== XSL ======================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="";>
  <xsl:output method="text" indent="no"/>

  <xsl:param name="ele">file_audio</xsl:param>

  <xsl:variable name="lc" select="'abcdefghijklmnopqrstuvwxyz'"/>
  <xsl:variable name="uc" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

  <xsl:key name="fn" match="file_audio"
  <xsl:key name="fn" match="file_avi" use="concat('file_avi','|',@fileref)"/>
  <xsl:key name="fn" match="file_graphic"

  <xsl:template match="/">
    <xsl:apply-templates select="*/frame/*[name() = $ele][generate-id() =

  <xsl:template match="@fileref">
    <xsl:value-of select="translate(., $uc, $lc)"/>
== end of XSL =================================

Hope this helps
Marrow - home of Xselerator (XSLT IDE and debugger)

 XSL-List info and archive:

Current Thread