Variables in XSLT

L

Lawrence

Hi

Is there any way of using variables in XSLT? I have been using
<xsl:param name="showDetails"/> and want to be able to set it to say 1
or 0 dependent on certain XSL:if statements throughout...is this
possible?

Kind Regards

Lawrence
 
M

Martin Honnen

Lawrence said:
Is there any way of using variables in XSLT? I have been using
<xsl:param name="showDetails"/> and want to be able to set it to say 1
or 0 dependent on certain XSL:if statements throughout...is this
possible?

Parameters are useful to pass values to templates e.g. if you declare
<xsl:template name="template-name">
<xsl:param name="param-name"/>
<!-- body of template comes here -->
</xsl:template>
then elsewhere in your stylesheet you can call that template and pass in
a value for the parameter e.g.
<xsl:call-template name="template-name">
<xsl:with-param name="param-name">
<xsl:choose>
<xsl:when test="some condition">some value</xsl:when>
<xsl:eek:therwise>some other value</xsl:eek:therwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>

Variables can be declared and bound to a value using e.g.
<xsl:variable name="variable-name" select="expression"/>
You can bind a value once, it is not possible to change that later.
So if you want to use some condition checks you need to do that when
using xsl:variable e.g.
<xsl:variable name="variable-name">
<xsl:choose>
<xsl:when test="some condition">some value</xsl:when>
<xsl:eek:therwise>some other value</xsl:eek:therwise>
</xsl:choose>
</xsl:variable>
 
D

Dimitre Novatchev

To add a little to Martin's explanation:

In case a variable should be bound to different node-sets depending on a
condition, the following is a useful pattern:

<xsl:variable name="myvarName"
select="$ns1[$cond]
|
$ns2[not($cond)]
"
/>

Cheers,
Dimitre Novatchev
 
J

Joe Kesselman

XSLT variables are single-assignment. You can't change them once they've
been set. The closest you can come is to create a new instance in a new
scope.

Generally the right thing to do is to rewrite to avoid the need. In the
few cases where you can't, recursion can be used in place of looping,
but that has serious efficiency issues and should generally be
considered a last resort.
 
W

Wizfrog

XSLT variables are single-assignment. You can't change them once they've
been set. The closest you can come is to create a new instance in a new
scope.

Generally the right thing to do is to rewrite to avoid the need. In the
few cases where you can't, recursion can be used in place of looping,
but that has serious efficiency issues and should generally be
considered a last resort.

One thing I have used if you want to generate 0 or 1 kind of trigger,
is use the position() function with modulo:

<xsl:when test="position() mod(2) = 0"> do this</
xsl:when><xsl:eek:therwise> do that</xsl:eek:therwise></xsl:choose>
 
W

Wizfrog

One thing I have used if you want to generate 0 or 1 kind of trigger,
is use the position() function with modulo:

<xsl:when test="position() mod(2) = 0"> do this</
xsl:when><xsl:eek:therwise> do that</xsl:eek:therwise></xsl:choose>

-> that will actually toggle ouput... I realize that may not be
exactly what you want.

then condition on the XPath as mentionned above is probably you best
bet: 2 different templates with different selection rules will run
seperately when the rule is met.
 
J

Joe Kesselman

Wizfrog said:
One thing I have used if you want to generate 0 or 1 kind of trigger,
is use the position() function with modulo:

or other math... Yep. If what you're looking for is a loop counter,
position() is often the best solution. Another approach is to write an
XPath that counts the preceding instances.
 
D

Dimitre Novatchev

Joe Kesselman said:
XSLT variables are single-assignment. You can't change them once they've
been set. The closest you can come is to create a new instance in a new
scope.

Generally the right thing to do is to rewrite to avoid the need. In the
few cases where you can't, recursion can be used in place of looping, but
that has serious efficiency issues

Not with good XSLT processors. Tail recursion optimization is something
standard in the world of Functional Programming.


I consider *harmful* any advice not to use recursion in XSLT.



Cheers,
Dimitre Novatchev
 
J

Joe Kesselman

Dimitre said:
Not with good XSLT processors. Tail recursion optimization is something
standard in the world of Functional Programming.

Unfortunately it is not yet standard in the world of XSLT processors.
Yes, there are some which do it, but I'm hesitant to recommend it
without knowing which processors are being targeted.

Also, most newbies aren't sophisticated enough to know what is and isn't
tail-recursion.
I consider *harmful* any advice not to use recursion in XSLT.

As indicated above, I consider that an overreaction. Especially since I
didn't intend to advise not to use recursion; my intent was to advise
against using using it when there are better XSLT idioms. In general, if
you just need a counter, recursion as a replacement for iteration is not
going to be the best solution.

There are exceptions.
 

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

No members online now.

Forum statistics

Threads
474,008
Messages
2,570,269
Members
46,871
Latest member
Stephendes

Latest Threads

Top