472,119 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Variables, and how to use them

I'm having trouble understanding the use of XSLT variables. I'm trying to
add up the values from a set of elements. The code currently looks like
this:

<xsl:variable name="orderTotal">
<xsl:value-of select="0" />
</xsl:variable>

<xsl:for-each select="item">
<xsl:variable name="orderTotal">
<xsl:value-of select="$orderTotal + qty*price" />
</xsl:variable>
</xsl:for-each>

<xsl:value-of select="$orderTotal" />

My 'item's each have a quantity and price, and I can print those values from
inside the for-each, so I know they're there to be used.

Under Linux, $orderTotal is always given as zero. Under Windows the XSLT
processor gives an error inside the loop saying 'orderTotal' cannot be
redefined.

How do I update the value of 'orderTotal' inside my loop?

Jul 20 '05 #1
8 1891
> This is a FFFAQ ...

My apologies. :o} Is there a FAQ for this NG?
XSLT is a functional (side-effects free) language and it is not possible
to change the value of an xsl:variable once initially defined.

You must use not xsl:for-each, but a named template that calls itself
recursively.


This is a concept I'm not familiar with. Is there anything out there I can
read to understand it?
Jul 20 '05 #2
Derek Fountain wrote:
This is a FFFAQ ...

My apologies. :o} Is there a FAQ for this NG?

XSLT is a functional (side-effects free) language and it is not possible
to change the value of an xsl:variable once initially defined.

You must use not xsl:for-each, but a named template that calls itself
recursively.

This is a concept I'm not familiar with. Is there anything out there I can
read to understand it?


There are a few examples at
http://www.dpawson.co.uk/xsl/sect2/recursion.html.

Below is an example that works with any source document. This example
uses a named template to implement a kind of 'for' loop. As you can see
from the result, the $from parameter value is incremented until the
"$from &lt; $to" test returns 'false' and the template is not called again.

-- begin stylesheet --

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:call-template name="for">
<!-- use the default value for $from -->
<xsl:with-param name="to" select="10"/>
<!-- use the default value for $step-->
</xsl:call-template>
</xsl:template>

<xsl:template name="for">

<xsl:param name="from" select="0"/>
<xsl:param name="to" select="0"/>
<xsl:param name="step" select="1"/>

<xsl:if test="$from &lt; $to">

<!-- Do something. Here, just output parameter values -->
<xsl:text>&#xA;&#xA;$from = </xsl:text>
<xsl:value-of select="$from"/>
<xsl:text>&#xA;$to = </xsl:text>
<xsl:value-of select="$to"/>
<xsl:text>&#xA;$to = </xsl:text>
<xsl:value-of select="$to"/>

<!-- Call itself with new parameter values -->
<xsl:call-template name="for">
<xsl:with-param name="from" select="$from + $step"/>
<xsl:with-param name="to" select="$to"/>
<xsl:with-param name="step" select="$step"/>
</xsl:call-template>

</xsl:if>

</xsl:template>

</xsl:stylesheet>

-- end stylesheet --
-- begin result --

$from = 0
$to = 10
$to = 10

$from = 1
$to = 10
$to = 10

$from = 2
$to = 10
$to = 10

$from = 3
$to = 10
$to = 10

$from = 4
$to = 10
$to = 10

$from = 5
$to = 10
$to = 10

$from = 6
$to = 10
$to = 10

$from = 7
$to = 10
$to = 10

$from = 8
$to = 10
$to = 10

$from = 9
$to = 10
$to = 10

-- end result --
// Magnus

Jul 20 '05 #3
Magnus Henriksson wrote:
Derek Fountain wrote:
This is a FFFAQ ...


My apologies. :o} Is there a FAQ for this NG?

XSLT is a functional (side-effects free) language and it is not possible
to change the value of an xsl:variable once initially defined.

You must use not xsl:for-each, but a named template that calls itself
recursively.


This is a concept I'm not familiar with. Is there anything out there I
can
read to understand it?

There are a few examples at
http://www.dpawson.co.uk/xsl/sect2/recursion.html.

Below is an example that works with any source document. This example
uses a named template to implement a kind of 'for' loop. As you can see
from the result, the $from parameter value is incremented until the
"$from &lt; $to" test returns 'false' and the template is not called again.


Stupid copy-paste-error. Here is what I intended:

-- begin stylesheet --

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:call-template name="for">
<!-- use the default value for $from -->
<xsl:with-param name="to" select="10"/>
<!-- use the default value for $step-->
</xsl:call-template>
</xsl:template>

<xsl:template name="for">

<xsl:param name="from" select="0"/>
<xsl:param name="to" select="0"/>
<xsl:param name="step" select="1"/>

<xsl:if test="$from &lt; $to">

<!-- Do something. Here, just output parameter values -->
<xsl:text>&#xA;&#xA;$from = </xsl:text>
<xsl:value-of select="$from"/>
<xsl:text>&#xA;$to = </xsl:text>
<xsl:value-of select="$to"/>
<xsl:text>&#xA;$step = </xsl:text>
<xsl:value-of select="$step"/>

<!-- Call itself with new parameter values -->
<xsl:call-template name="for">
<xsl:with-param name="from" select="$from + $step"/>
<xsl:with-param name="to" select="$to"/>
<xsl:with-param name="step" select="$step"/>
</xsl:call-template>

</xsl:if>

</xsl:template>

</xsl:stylesheet>

-- end stylesheet --
-- begin result --

$from = 0
$to = 10
$step = 1

$from = 1
$to = 10
$step = 1

$from = 2
$to = 10
$step = 1

$from = 3
$to = 10
$step = 1

$from = 4
$to = 10
$step = 1

$from = 5
$to = 10
$step = 1

$from = 6
$to = 10
$step = 1

$from = 7
$to = 10
$step = 1

$from = 8
$to = 10
$step = 1

$from = 9
$to = 10
$step = 1

-- end result --
// Magnus

Jul 20 '05 #4

"Derek Fountain" <no****@hursley.ibm.com> wrote in message
news:3f***********************@freenews.iinet.net. au...
This is a FFFAQ ...


My apologies. :o} Is there a FAQ for this NG?
XSLT is a functional (side-effects free) language and it is not possible
to change the value of an xsl:variable once initially defined.

You must use not xsl:for-each, but a named template that calls itself
recursively.


This is a concept I'm not familiar with. Is there anything out there I can
read to understand it?


See for example:

http://www-106.ibm.com/developerwork...ur/?loc=dwmain
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
Jul 20 '05 #5
On Thu, 21 Aug 2003 22:24:39 +0800, Derek Fountain
<no****@hursley.ibm.com> wrote:
This is a concept I'm not familiar with.


Think of it this way (which is extremely partial), but not a bad way
to approach it.

Variables in XSLT cannot be modified once set.

Variables in XSLT are typed (in some ways very strongly) to contain a
set of nodes. XSLT sees these nodes as atomic, and manipulating the
values _within_ these nodes is not something that it's appropriate
for.

So assemble a set of the nodes you want, as an XSLT variable, then use
something like a JavaScript extension to operate upon them.

There's also the "functional programming" approach. You still can't
modify variables, but you can work with the contents of a stack.

Write a recursive template that traverses the set of nodes you want
(get this working first). Then give it a parameter, and a local
variable. Pass the sum of these two as the actual parameter to the
next call. When you reach the end, return the value accumulated.

After doing this for a while, you'll probably grow a beard and start
pining for Lisp !

You'll also find that code in this style is dificult to maintain by
other team members. Code with extensions often survives better,
although excessive use of them can turn ugly too.
If you don;t already have it, Michael Kay's XSLT book is a good one.

Jul 20 '05 #6
>
So assemble a set of the nodes you want, as an XSLT variable, then use
something like a JavaScript extension to operate upon them.


Using any extension function with side effects is dangerous,
unpredictable/unreliable and should not be recommended.
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
Jul 20 '05 #7
On Thu, 21 Aug 2003 18:01:09 +0200, "Dimitre Novatchev"
<dn********@yahoo.com> wrote:
Using any extension function with side effects is dangerous,
unpredictable/unreliable and should not be recommended.


Side effects are certainly a bad thing. I was thinking more of some
global state, accessed only through a few extension functions (clear,
accumulate, get total) and then returned by calling a function.

Certainly I would be most cautious about any extension function that
had side-effects _that_affected_the_document_. But I don't think we
need that here,

Jul 20 '05 #8
> Certainly I would be most cautious about any extension function that
had side-effects _that_affected_the_document_. But I don't think we
need that here,


Not only "that affected the document"...

Any keeping and modifying of "program state" (e.g. using static Javascript
variables for counters/accumulators) will have unpredictable results.

See for real practical examples these:

http://groups.google.com/groups?hl=e...TK2MSFTNGP09&r
num=7&prev=/groups%3Fq%3Ddimitre%2Bxslt%2Bfunctions%2Bside%2Be ffects%26hl%3D
en%26lr%3D%26ie%
3DUTF-8%26selm%3DuASG4eE2CHA.1560%2540TK2MSFTNGP09%26rnu m%3D7
(read the whole thread)

http://groups.google.com/groups?hl=d...ecc2e2c&seekm=
uZk9iKYiBHA.2000%40tkmsftngp04#link1
=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael | last post: by
12 posts views Thread by scott | last post: by
6 posts views Thread by Clausfor | last post: by
5 posts views Thread by Sandman | last post: by
41 posts views Thread by none | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.