Re: [xsl] Slow XSLT

Subject: Re: [xsl] Slow XSLT
From: "Manfred Staudinger" <manfred.staudinger@xxxxxxxxx>
Date: Fri, 14 Mar 2008 19:37:27 +0100
On 14/03/2008, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> > A lot has already been said and many suggestions have already
>  > been made in this thread to speed up the processing of your
>  > stylesheet, but since your stylesheet is rather
>  > straightforward I doubt if any of the suggestions really
>  > speed up the processing.
>
>
> I agree, these are all things that might give you 1% improvement. The only
>  other thing I can suggest is looking at the format-number() calls. I've
>  known that be really slow on some implementations. Try taking it out and
>  seeing if it makes any difference.

I would like to propose to move the formating to the css as much as
possible (that means all what is implemented by the browsers you want
to support - this is a lot but OT on this list). Begin your
measurements with the stylesheet below and observe carefully any
changes when you add "string-formating". I use SAXON 6.5.5 with the -t
switch to get the timings. I've changed a few things, if you have
questions don't hesitate to ask.

Manfred

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<!--
java -jar D:\Programme\Saxon6\saxon.jar -t slow0.xml sslow2.xsl
-->
<xsl:param name="axisHeads" select="'true'"/>
<xsl:output indent="yes"/>

<xsl:variable name="msrs" select="count(/Reports/Report/Measures/Measure)"/>

<xsl:template match="/">
	<html>
	<body>
		<table border="1">
		<tbody>
			<xsl:choose>
				<xsl:when test="$axisHeads = 'true'">
					<xsl:call-template name="apply-set">
						<xsl:with-param name="set" select="Reports/Report/Columns/*"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="apply-set">
						<xsl:with-param name="set" select="Reports/Report/Columns/*/*"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="Reports/Report/Rows/*/*"/>
		</tbody>
		</table>
	</body>
	</html>
</xsl:template>

<xsl:template match="Row">
	<tr>
		<xsl:if test="$axisHeads = 'true'">
			<th><xsl:value-of select="@heading"/></th>
		</xsl:if>
		<xsl:apply-templates/>
	</tr>
</xsl:template>
<xsl:template match="Cell">
	<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Msr">
	<td><xsl:value-of select="@val"/></td>
</xsl:template>
<xsl:template match="Cell[not(*)]">
	<xsl:for-each select="/Report/Measures/Measure">
		<td/>
	</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>

<xsl:template name="apply-set">
	<xsl:param name="set"/>
	<tr>
		<xsl:if test="$axisHeads = 'true'">
			<th/>
		</xsl:if>
		<xsl:apply-templates select="$set"/>
	</tr>
	<xsl:choose>
		<xsl:when test="$set/*">
			<xsl:call-template name="apply-set">
				<xsl:with-param name="set" select="$set/*"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<tr valign="bottom">
				<xsl:if test="$axisHeads = 'true'">
					<th/>
				</xsl:if>
				<xsl:for-each select="$set">
					<xsl:apply-templates select="/Reports/Report/Measures/*">
						<xsl:with-param name="pos" select="position()"/>
					</xsl:apply-templates>
				</xsl:for-each>
			</tr>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="ColGrp">
	<th colspan="{$msrs*count(.//Col)}">
		<div><xsl:value-of select="@heading"/></div>
	</th>
</xsl:template>
<xsl:template match="Col">
	<th colspan="{$msrs}">
		<div><xsl:value-of select="@heading"/></div>
	</th>
</xsl:template>
<xsl:template match="Measure">
	<xsl:param name="pos" />
	<th>
		<div onclick="sortFullGrid({position()}, {$pos}, '')">
			<xsl:value-of select="@heading"/>
		</div>
	</th>
</xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<Reports>
  <Report>
   <Measures>
     <Measure idx="1" heading="Total Pages"/>
     <Measure idx="2" heading="Cost"/>
   </Measures>
   <Columns>
     <ColGrp heading="Year">
       <Col heading="2003"/>
       <Col heading="2004"/>
     </ColGrp>
   </Columns>
   <Rows>
     <RowGrp heading="Name 1">
       <Row heading="A">
         <Cell/>
         <Cell>
           <Msr idx="1" val="42.00"/>
           <Msr idx="2" val="64230"/>
         </Cell>
       </Row>
       <Row heading="Other">
         <Cell/>
         <Cell>
           <Msr idx="1" val="36.00"/>
           <Msr idx="2" val="35820"/>
         </Cell>
       </Row>
       <Row heading="Another">
         <Cell>
           <Msr idx="1" val="14.20"/>
           <Msr idx="2" val="128030"/>
         </Cell>
         <Cell/>
       </Row>
     </RowGrp>
     <RowGrp heading="">
       <Row heading="Totals">
         <Cell>
           <Msr idx="1" val="14.20"/>
           <Msr idx="2" val="128030.00"/>
         </Cell>
         <Cell>
           <Msr idx="1" val="78.00"/>
           <Msr idx="2" val="100050.00"/>
         </Cell>
       </Row>
     </RowGrp>
   </Rows>
 </Report>
</Reports>

Output HTML:

<html>
   <body>
      <table border="1">
         <tbody>
            <tr>
               <th></th>
               <th colspan="4">
                  <div>Year</div>
               </th>
            </tr>
            <tr>
               <th></th>
               <th colspan="2">
                  <div>2003</div>
               </th>
               <th colspan="2">
                  <div>2004</div>
               </th>
            </tr>
            <tr valign="bottom">
               <th></th>
               <th>
                  <div onclick="sortFullGrid(1, 1, '')">Total Pages</div>
               </th>
               <th>
                  <div onclick="sortFullGrid(2, 1, '')">Cost</div>
               </th>
               <th>
                  <div onclick="sortFullGrid(1, 2, '')">Total Pages</div>
               </th>
               <th>
                  <div onclick="sortFullGrid(2, 2, '')">Cost</div>
               </th>
            </tr>
            <tr>
               <th>A</th>
               <td>42.00</td>
               <td>64230</td>
            </tr>
            <tr>
               <th>Other</th>
               <td>36.00</td>
               <td>35820</td>
            </tr>
            <tr>
               <th>Another</th>
               <td>14.20</td>
               <td>128030</td>
            </tr>
            <tr>
               <th>Totals</th>
               <td>14.20</td>
               <td>128030.00</td>
               <td>78.00</td>
               <td>100050.00</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

Current Thread