Re: [xsl] Read the data only once.

Subject: Re: [xsl] Read the data only once.
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 9 Apr 2002 08:20:58 +0100
Hi Raj,

> In my xsl I have a global varaible which caluculates and holds some
> data. I am using that variable from all over the xsl. I am making
> calls to named templates while creating the variable. Each time I
> call the varialbe, that named templates are being called to extract
> the value. I need a different behaviour here. In my case that
> variable remains constant through out the xsl. So I want to
> calculate the variable only once. Is there a easy way to implement
> this.

I assume that you mean you have something like:

<xsl:variable name="header">
  <xsl:call-template name="createHeader" />
</xsl:variable>

<xsl:template name="createHeader">
  ...
</xsl:template>

Is that correct?

XSLT is, by definition, side-effect free, which means that the same
code can be executed multiple times and always give the same value.
The only thing that I can think of which would make the variable
change is if you are using some extensions within your named template
and those extensions have side-effects. Is that what you're doing?

The trouble is that because XSLT is designed to be side-effect free,
processors make optimisations when executing the stylesheet that
depend on this feature -- they might calculate the value of a variable
more than once to save the memory it would take to store the result,
or only call a function once with the same set of arguments because
they already know what the answer's going to be. Because the results
of evaluating something in XSLT cannot change, by definition, this
doesn't matter.

So basically, no, there isn't an easy way to tell your processor to
forget everything it knows about the fundamental rules of XSLT. What
you should do is redesign your stylesheet so that the information
that's being generated by the extensions is calculated prior to the
transformation and passed in to the stylesheet through parameters or
external files instead.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread