Re: [xsl] How do i optimize my stylesheet for speed?

Subject: Re: [xsl] How do i optimize my stylesheet for speed?
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 15 Jun 2001 16:08:52 +0100
Hi Wajihuddin,

> I have written a stylesheet that takes a catalog from an application
> in xml format and converts to a csf (character seperated file) text
> file. On a single CPU ultarsparc 248MHz, 384MB RAM, with 128MB of
> heap size it is taking about 10 min for a 5MB input file. However on
> a 4CPU (400MHz) box it is just taking a megre 42 sec. What can i do
> to make it faster on the slow machine?

I don't know if this will help, but you could try rewriting the
stylesheet so that it works down the tree to generate the output
rather than up it. At the moment, you're probably doing the same thing
(traversing a set of ancestors) several times.

You need a default template that takes a parameter giving the path
collected from its ancestors, and adds its own name to that path,
which it passes on to the next level:

<xsl:template match="*">
   <xsl:param name="path" />
   <xsl:apply-templates select="*">
      <xsl:with-param name="path">
         <xsl:value-of select="$path" />|<xsl:value-of select="@name" />

The only exception to this is the Item elements whose parent has a
'vortex-type' attribute of 'category'.  There, you want to actually
output whatever the path is (with extra information on the end) before
moving on to the Item's children:

<xsl:template match="*[@vortext-type]/Item">
   <xsl:param name="path" />
   <xsl:variable name="new-path">
      <xsl:value-of select="$path" />|<xsl:value-of select="@name" />
   <xsl:for-each select="Attribute">
      <xsl:value-of select="@value" />
      <xsl:if test="position()!=last()">
   <xsl:value-of select="$new-path" />
   <xsl:apply-templates select="*">
      <xsl:with-param name="path" select="$new-path" />

You may be able to optimise this further with your knowledge of the
XML structure that you're working with.  For example, are there ever
any Item elements that you're interested in nested within other Item
elements?  If not, then take away the last xsl:apply-templates in the
second template above.  If there are other 'dead ends' in your XML
structure, you should prune them as high up as possible.

> Also in the output, on the 1st row i want to get the name of the
> attributes (not values). So that i have a column heading for the
> whole file. How can i do that? I tried using counters and some logic
> with when-otherwise but failed.

Do all the Item elements in your source have the same set of Attribute
elements underneath them?  If so, get the first Item element with:


And then iterate over its child Attribute elements accessing their
@name attribute.

If they don't, then it's a grouping problem and you'd be best off
with a Muenchian solution.  See for more details, or ask
here again.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread