Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Variables in XSLT

Reply
Thread Tools

Variables in XSLT

 
 
Lawrence
Guest
Posts: n/a
 
      07-13-2007
Hi

Is there any way of using variables in XSLT? I have been using
<xslaram 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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-13-2007
Lawrence wrote:

> Is there any way of using variables in XSLT? I have been using
> <xslaram 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">
<xslaram 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>
<xsltherwise>some other value</xsltherwise>
</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>
<xsltherwise>some other value</xsltherwise>
</xsl:choose>
</xsl:variable>


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-14-2007
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" <(E-Mail Removed)> wrote in message
news:46977a53$0$3842$(E-Mail Removed)-online.net...
> Lawrence wrote:
>
>> Is there any way of using variables in XSLT? I have been using
>> <xslaram 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">
> <xslaram 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>
> <xsltherwise>some other value</xsltherwise>
> </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>
> <xsltherwise>some other value</xsltherwise>
> </xsl:choose>
> </xsl:variable>
>
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/



 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      07-14-2007
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
 
Reply With Quote
 
Wizfrog
Guest
Posts: n/a
 
      07-14-2007
On Jul 13, 8:55 pm, Joe Kesselman <(E-Mail Removed)> wrote:
> 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><xsltherwise> do that</xsltherwise></xsl:choose>

 
Reply With Quote
 
Wizfrog
Guest
Posts: n/a
 
      07-14-2007
> 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><xsltherwise> do that</xsltherwise></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.

 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      07-14-2007
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
 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-15-2007

"Joe Kesselman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      07-15-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments