xsl-list
[Top] [All Lists]

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

2012-11-03 13:12:29
Great explanation, Ken.

To make this clear to "classical" programmers, Approach 1 fully
follows the "Open-Closed" principle [1] of OOP (which is the "O" in
SOLID [2]) -- open for extension and closed for modification.

On the other side, Approach 2 cannot be extended without modification.


Cheers,

Dimitre

[1]. Wikipedia: Open/Closed Principle , at:
http://en.wikipedia.org/wiki/Open/closed_principle

[2]. Wikipedia: SOLID (object-oriented design), at:
http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)



On Sat, Nov 3, 2012 at 10:58 AM, G. Ken Holman
<gkholman(_at_)cranesoftwrights(_dot_)com> wrote:

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>

<template match="thenode[not(child::x)]">
   … do_that …
¦
</template>


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>
</xsl:choose>
</template>


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(_at_)CraneSoftwrights(_dot_)com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--