[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Optimisation -- premature?

Subject: Re: Optimisation -- premature?
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 12 Feb 2007 09:53:08 +0000
Re:  Optimisation -- premature?
On 2/11/07, John Horner <Horner.John@xxxxxxxxxx> wrote:
I have a transformation which sometimes, depending on circumstances,
creates bunch of variables used in an external function.

Let's say that logic is like this:

<xsl:if test="a_certain_flag_is_set">
   <xsl:variable name="foo" select="foo"/>
   <xsl:variable name="bar" select="bar"/>
   <xsl:variable name="baz" select="baz"/>
   <xsl:value-of select="namespace:function($foo,$bar,$baz)"/>
</xsl:if>

That's OK except that elsewhere in the stylesheet, if a different and
mutually exclusive flag is set, I need to create exactly the same
variables

<xsl:if test="a_different_flag_is_set">
   <xsl:variable name="foo" select="foo"/>
   <xsl:variable name="bar" select="bar"/>
   <xsl:variable name="baz" select="baz"/>
   <xsl:value-of select="namespace:function($foo,$bar,$baz)"/>
</xsl:if>

I want to be more efficient about this, and would rather do this at the
top of the stylesheet

<xsl:if test="a_certain_flag_is_set
              or
              a_different_flag_is_set">
   <xsl:variable name="foo" select="foo"/>
   <xsl:variable name="bar"select="bar"/>
   <xsl:variable name="baz" select="baz"/>
</xsl:if>

That is, create them once, if they're going to be needed by either
function later in the processing. Because of the way scope works in XSL,
I can't do this, can I? So, which of the two less efficient methods is
best -- create them every time the process runs, although I may not need
them, or duplicate their creation locally in the separate if-elements?

It's not a problem as such, the process works just fine, but it's
bugging me a little that I can't do it the "right" way.

It's a bit hard to say going on the samples you've provided, but some suggestions are:

- Separate out the duplicate code into named templates or functions
(in 2.0).  Usually named templates are best when element creation is
needed, functions are best for computing values.

- If you have distinct choose/when/otherwise blocks then separate them
out into specific stylesheets that import a common stylesheet.

- Define the common variables "higher up" and use tunneling to get
them to the templates where they're used (again a 2.0 feature).

cheers
andrew

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.