473,608 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variables in XSLT

Hi

Is there any way of using variables in XSLT? I have been using
<xsl:param name="showDetai ls"/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 3447
Lawrence wrote:
Is there any way of using variables in XSLT? I have been using
<xsl:param name="showDetai ls"/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="express ion"/>
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*******@yaho o.dewrote in message
news:46******** **************@ newsspool4.arco r-online.net...
Lawrence wrote:
>Is there any way of using variables in XSLT? I have been using
<xsl:param name="showDetai ls"/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="express ion"/>
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:o therwisedo 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:o therwisedo 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.netwro te 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
2018
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"> <xsl:value-of select="0" /> </xsl:variable> <xsl:for-each select="item"> <xsl:variable name="orderTotal">
8
2353
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 - the included file depends on the language. Atm I'm editing those file manually. Luckily atm it's only two languages I have to work with, but even in this case I forget to add a variable that I added in the other file, forget to use entities .....
9
2423
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 already sucsessfully. However , the rest does not work as expected. Hope someone can help on that. Rolf
1
2282
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 0x if nessesary. If there is some idea to deal with leading zeros it would be even better. Thanks a lot for your ideas and comments
0
2310
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 stuff with actions and selectors and stuff, but I can't find a simple explanation of how variables in the sitemap actually work. Can anyone direct me to a good article on that? Another problem I'm having is that for some strange reason, my...
1
564
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 certain conditions (not always). It can't be calculated at the beginning of the file
4
9280
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 to the xslt along with a parameter and get xhtml returned. The catch is that I'm not finding any samples of how to do that. I can find (as usual) some C# examples, and a few VB.NET for client apps samples, but nothing for ASP.NET using VB.NET.
6
4685
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'/> <options> <USER> <NAME>Bob</NAME>
2
1209
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 people) to display some statistics about these error codes. Specifically, how many of each type there are. My thought was to create some code using variables, and do $err01+1 (or ++), but as far as I can tell you are not allowed to manipulate...
5
1662
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 name and quantity). The composition never changes, only the price can change. I can calculate the total value (price*quantity) with a for-each loop (using xsl:key) for each set of components, listing below.
0
8504
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
8489
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...
0
8362
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
6833
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
5493
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
3981
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2486
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
1
1614
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1349
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.