[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Perfomance: 'conditional instruction' vs. 'multi template'

Subject: RE: [xsl] Perfomance: 'conditional instruction' vs. 'multi template'
From: Daniel Sullivan <dsullivan@xxxxxxxxx>
Date: Sat, 3 Nov 2012 12:42:44 -0600

But templates in the importing stylesheet that match have absolute priority
over those in the imported stylesheet, so a declarative stylesheet that
imports another stylesheet would have the same effect whether the imported
stylesheet was declarative or imperative, wouldn't it?

For example:

<import href="imperative.xslt">
<template match="thenode[child::x]">
   ... do_this ...

Would have the same effect as

<import href="declarative.xslt">
<template match="thenode[child::x]">
   ... do_this ...

Are they any cases where these would produce different results?

Certainly in the general case it's probably harder to understand what the
override will do when the imported stylesheet is imperative, but you can use
declarative template to override an imperative one.

In the past I've used declarative stylesheets to import and fix imperative
stylesheets that I wasn't allowed to change.


-----Original Message-----
From: G. Ken Holman [mailto:g.ken.holman@xxxxxxxxx] On Behalf Of G. Ken
Sent: Saturday, November 03, 2012 1:59 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx; xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Perfomance: 'conditional instruction' vs. 'multi template'

At 2012-11-03 16:24 +0100, Norbert Heidbrink wrote:
>if a node is to be processed differently, depending on a certain
>condition, there are two approaches.
>Let's say, the condition is "existance of child x".
>Approach 1: two templates with different matching XPaths
><template match="thenode[child::x]">
>   ... do_this ...
><template match="thenode[not(child::x)]">
>    ... do_that ...
>Approach 2: one template with a conditional instruction
><template match="thenode">
>   <xsl:choose>
>   <xsl:when test="x">
>     ... do_this ...
>   </xsl:when>
>   <xsl:otherwise>
>     ... do_that ...
>   </xsl:otherwise>
>I wonder, if one of these approaches is to be favoured?
>Are there any severe implications on performance?
>Any other advantages / disadvantages that speak in favour of approach 1
>or approach 2?
>I am curious to read your estimations,

I am not an implementer, just a user, but this is how I see it and teach it.

Approach 1 allows the processor to optimize the method by which it decides
between the two template rules each with a template to build the result tree.

Approach 2 burdens the processor to execute your own determination of how to
choose between the two templates to build the result tree.

Approach 1 is declarative, approach 2 is imperative.

Approach 2 may be more intuitive to classical programmers, approach 1 may be a
bit harder for classical programmers to wrap their heads around.

Approach 1 can be exploited by an importing stylesheet in order to effect
nuanced differences in the stylesheet for one of the two template rules, even
when the imported stylesheet is read-only.  Approach 2 prevents nuanced
overriding of only one of the two templates and imposes the burden on the
importing stylesheet to replicate the behaviours of the other template already
in the imported stylesheet that don't need to change.

For these reasons, especially the last one, I tell my students approach 1 is
"better" and approach 2 has drawbacks.  I always try to write my stylesheets
so that in the future I can better exploit the stylesheet without changing it,
even if I don't yet know what those future requirements might be.  That allows
me to continue using the unchanged stylesheet wherever I am using it, and yet
leverage it for other reasons in other contexts by the importing stylesheet
selectively changing behaviours.  Top-level constructs can be overridden in
their entirety, while their contents cannot be selectively overridden.  The
more template rules I have, the more nuanced I can change the behaviours.  The
fewer template rules I have, the less flexibility I have to override smaller
portions of the imported stylesheet.

I hope this helps.

. . . . . . . . . . Ken

Contact us for world-wide XML consulting and instructor-led training Free
5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal

Current Thread