473,387 Members | 1,483 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,387 software developers and data experts.

Variables in XSLT

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

Jul 13 '07 #1
8 3412
Lawrence wrote:
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:otherwise>some other value</xsl:otherwise>
</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:otherwise>some other value</xsl:otherwise>
</xsl:choose>
</xsl:variable>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 13 '07 #2
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
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:46**********************@newsspool4.arcor-online.net...
Lawrence wrote:
>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:otherwise>some other value</xsl:otherwise>
</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:otherwise>some other value</xsl:otherwise>
</xsl:choose>
</xsl:variable>
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 14 '07 #3
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jul 14 '07 #4
On Jul 13, 8:55 pm, Joe Kesselman <keshlam-nos...@comcast.netwrote:
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
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:otherwisedo that</xsl:otherwise></xsl:choose>

Jul 14 '07 #5
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:otherwisedo that</xsl:otherwise></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.

Jul 14 '07 #6
Wizfrog wrote:
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jul 14 '07 #7

"Joe Kesselman" <ke************@comcast.netwrote in message
news:aY******************************@comcast.com. ..
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
and should generally be considered a last resort.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry

Jul 15 '07 #8
Dimitre Novatchev wrote:
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jul 15 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Derek Fountain | last post by:
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">...
8
by: Sebastian Kerekes | last post by:
Greetings, I'm developing an application that supports multiple languages. In my XSL I use variables to place the text where it belongs to. At the top of the document I include those variables -...
9
by: Rolf Kemper | last post by:
Dear Experts, I got stuck with the following problem and need your help. What I wnat to do is to get a set of distinct nodes. Before the distinct I have selected the multiple occourences...
1
by: Rolf Kemper | last post by:
Dear All, does have anybody have an idea how to add to number strings in xslt ? My strings are in variables eg. '1A2' , '00F' which should sum up to '1B1'. I may prefix the variables with...
0
by: mcv | last post by:
I'm currently learning to use Cocoon, and although I really like it, there are a couple of things that don't seem to be explained very well. I've seen lots of articles explaining really complicated...
1
by: Oleg Konovalov | last post by:
Hi, I am new to XSLT, trying to significantly modify somebody else's XSL. That is not 2.0. I need to create min & max variable(s) to be used in many templates and sub-templates based on...
4
by: darrel | last post by:
I think I've gotten the hang of XML and XSLT...at least enough to produce the results I'm looking for. I now need to take the next step and use ASP.NET (using VB.NET) to take the xml and send it...
6
by: Mike Grass | last post by:
Hi, I have an XML file similar to the following: <!-- snippet --> <selector key='USER/id' value='type1'/> <selector key='USER/id' value='type2'/> <selector key='USER/id' value='type3'/>...
2
by: requeth | last post by:
I have 12 possible error codes that can be placed in an attribute, and multiple attributes with this in an XML file. I am attempting to use an XSLT (which makes the data viewable to non technical...
5
by: the_jos | last post by:
Dear reader, I am trying some things with xml/xsl and cannot find a solution for what I would like to do. I have 2 base items with name and price and two that are composed of base two (given...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.