Variables in XSLT

Discussion in 'XML' started by Lawrence, Jul 13, 2007.

  1. Lawrence

    Lawrence Guest

    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
    Lawrence, Jul 13, 2007
    #1
    1. Advertising

  2. 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: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>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jul 13, 2007
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:46977a53$0$3842$-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: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>
    >
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
    Dimitre Novatchev, Jul 14, 2007
    #3
  4. 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
    Joe Kesselman, Jul 14, 2007
    #4
  5. Lawrence

    Wizfrog Guest

    On Jul 13, 8:55 pm, Joe Kesselman <> 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><xsl:eek:therwise> do that</xsl:eek:therwise></xsl:choose>
    Wizfrog, Jul 14, 2007
    #5
  6. Lawrence

    Wizfrog Guest

    > 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.
    Wizfrog, Jul 14, 2007
    #6
  7. 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
    Joe Kesselman, Jul 14, 2007
    #7
  8. "Joe Kesselman" <> wrote in message
    news:...
    > 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
    Dimitre Novatchev, Jul 15, 2007
    #8
  9. 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
    Joe Kesselman, Jul 16, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    640
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,662
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    615
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    708
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    9
    Views:
    931
Loading...

Share This Page