473,326 Members | 2,095 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,326 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 3401
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.