473,394 Members | 1,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2000
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
12
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE...
6
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
18
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.