Re: [xsl] Antwort: RE: [xsl] bad programming for speedup?

Subject: Re: [xsl] Antwort: RE: [xsl] bad programming for speedup?
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Tue, 24 Jul 2007 14:46:09 +0200
Hi Christoph,

Have you tried the other suggestions? Mainly Andrew's method (or mine even, if the below does what I think it does).

Please allow me to make some suggestions on your code below:



christoph.naber@xxxxxxxxxxxxxxxxxxx wrote:
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>



This is not necessary in this particular template. However, you may feel that having a specific entry point in your code is a matter of good practice (many do).


<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="*" /> </xsl:copy>
</xsl:template>

To save yourself some keystrokes (also further on), you can as well write the above as follows:


<xsl:template match="node() | @*">
   <xsl:copy>
      <xsl:appy-templates select="node() | @*" />
   </xsl:copy>
</xsl:template>

In addition, this way (using node() instead of *), text nodes, processing instructions etc are also processed by your copy template.

<xsl:template match="row[position() = 1 or preceding-sibling::*[name() != 'row' and position() = 1]]" >
<table>
<xsl:apply-templates select="." mode="more2come"/>
</table>
</xsl:template>

Take a close look at the way Andrew coded this same principle. Also, consider an earlier remark by Justin about *not* using name() to compare nodes. Use self::row instead. This will help you when you are attempting sources that contain namespaces.


<xsl:template match="row" mode="more2come">
<xsl:copy>
<xsl:copy-of select="@*" />

The line above (xsl:copy-of) can be removed if you follow the remark made above about the copy template.


<xsl:apply-templates select="*" />

And this should really read:


<xsl:apply-template select="node() | @*" />

to make it all work well.

</xsl:copy>
<xsl:if test="following-sibling::*[name() = 'row' and position() = 1]">
<xsl:apply-templates select="following-sibling::row[position() = 1]" mode="more2come" />
</xsl:if>

Whenever you find yourself making an xsl:if with a node test, followed by an xsl:apply-templates with (largely) the same call, it is most likely that you can easily write it in one statement and make it clearer for you and fellow coders. Note that xsl:apply-templates does nothing when the selection contains an empty set, making the xsl:if from imperative programming style largely redundant.


See the code of Andrew for an example.


</xsl:template>
<xsl:template match="row" >
</xsl:template>

Using the approach of Andrew, you do not need this delete-template, saving on keystrokes again ;)


If you want it simple and fast, I believe the approach with keys is quicker, but not necessarily so of course. In addition, since all you do is plain copying and no further processing, it is likely that the <xsl:copy-of> constructor from my example will work the fastest nonetheless. But I haven't seen your source, so I couldn't tell (and with an optimizing processor it may matter zero after all).

Cheers & happy coding ;)

-- Abel Braaksma

Current Thread