Variables, and how to use them

D

Derek Fountain

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?
 
D

Derek Fountain

This is a FFFAQ ...

My apologies. :eek:} 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?
 
M

Magnus Henriksson

Derek said:
My apologies. :eek:} Is there a FAQ for this NG?




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:eek:utput 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>

$from = </xsl:text>
<xsl:value-of select="$from"/>
<xsl:text>
$to = </xsl:text>
<xsl:value-of select="$to"/>
<xsl:text>
$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
 
M

Magnus Henriksson

Magnus said:
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:eek:utput 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>

$from = </xsl:text>
<xsl:value-of select="$from"/>
<xsl:text>
$to = </xsl:text>
<xsl:value-of select="$to"/>
<xsl:text>
$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
 
A

Andy Dingley

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.
 
D

Dimitre Novatchev

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
 
A

Andy Dingley

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,
 
D

Dimitre Novatchev

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=en&lr=&ie=UTF-8&threadm=uASG4eE2CHA.1560@TK2MSFTNGP09&r
num=7&prev=/groups%3Fq%3Ddimitre%2Bxslt%2Bfunctions%2Bside%2Beffects%26hl%3D
en%26lr%3D%26ie%
3DUTF-8%26selm%3DuASG4eE2CHA.1560%2540TK2MSFTNGP09%26rnum%3D7
(read the whole thread)

http://groups.google.com/groups?hl=de&frame=right&th=418985883ecc2e2c&seekm=
uZk9iKYiBHA.2000%40tkmsftngp04#link1


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top