[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Efficiency, use param in key?

Subject: Re: Efficiency, use param in key?
From: "Marrow" <marrow@xxxxxxxxxxxxxx>
Date: Sat, 3 Aug 2002 11:02:41 +0100
xsl key topxml
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() =
generate-id(key('fn',@fileref)[1])]">

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() =
generate-id(key('fn',@fileref)[1])]">
    <xsl:sort select="@fileref"/>
  </xsl:apply-templates>

(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
>template?

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="http://www.w3.org/1999/XSL/Transform">
  <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"
use="concat('file_audio','|',@fileref)"/>
  <xsl:key name="fn" match="file_avi" use="concat('file_avi','|',@fileref)"/>
  <xsl:key name="fn" match="file_graphic"
use="concat('file_graphic','|',@fileref)"/>

  <xsl:template match="/">
    <xsl:apply-templates select="*/frame/*[name() = $ele][generate-id() =
generate-id(key('fn',concat(name(),'|',@fileref))[1])]/@fileref">
      <xsl:sort/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="@fileref">
    <xsl:value-of select="translate(., $uc, $lc)"/>
    <xsl:text>&#xD;&#xA;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
== end of XSL =================================

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.