473,659 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="orderTota l">
<xsl:value-of select="0" />
</xsl:variable>

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

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

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 2020
> 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:styleshe et 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:styleshe et 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********@yah oo.com> wrote:
Using any extension function with side effects is dangerous,
unpredictabl e/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%3Ddi mitre%2Bxslt%2B functions%2Bsid e%2Beffects%26h l%3D
en%26lr%3D%26ie %
3DUTF-8%26selm%3DuASG 4eE2CHA.1560%25 40TK2MSFTNGP09% 26rnum%3D7
(read the whole thread)

http://groups.google.com/groups?hl=d...ecc2e2c&seekm=
uZk9iKYiBHA.200 0%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
5613
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
7
3129
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 couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
28
4611
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. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would only have to be created once. Can anyone confirm this. Thanks. -- Dennis in Houston
12
6242
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 *********************************** set objRS = GetMyRecordSet() i=1 objRS.MoveFirst
6
3219
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 automatic non volatile variables of the function invoking setjmp, these will be undefined if modified after the setjmp call"
18
3432
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 File-New-Window creates an instance of IE in the same process with the same SessionID as the parent window is in big trouble. This fundamentally restricts the usefullness of using session state management. I probably missed it somewhere - can...
9
8644
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 Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
11811
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 $MYVAR, $a; ?>
1
29343
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
41
2529
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, Ami
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.