xsl:variable is Constant or really Variable?

Discussion in 'XML' started by Afshar Mohebbi, Jun 25, 2006.

  1. Hi everybody there,
    I've declared a <xsl:variable name="something" select="999"/> in my
    xslt and when I want to set another value in it (with same syntax) I
    get error the variable "something" has been declared previously. I
    couldn't find a way to change it's value. It seems it is constant.
    Is there any alternative way?

    Thanks in Advance
    Afshar
     
    Afshar Mohebbi, Jun 25, 2006
    #1
    1. Advertising

  2. Afshar Mohebbi wrote:


    > I've declared a <xsl:variable name="something" select="999"/> in my
    > xslt and when I want to set another value in it (with same syntax) I
    > get error the variable "something" has been declared previously. I
    > couldn't find a way to change it's value. It seems it is constant.
    > Is there any alternative way?


    XSLT is a pure functional language so you can bind a value to a variable
    once but not change that afterwards.
    As for alternative ways, what exactly are you trying to achieve?

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 25, 2006
    #2
    1. Advertising

  3. In XSLT, all variables are single-assignment. You can mask them with
    another variable having the same name in a deeper scope, but when you
    exit that scope you're looking at the original variable again. (This is
    a direct consequence of XSLT being designed as a pure functional
    language, which has implications for efficiency.)

    Yes, this requires that you change your coding style to fit the
    language's capabilities.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 25, 2006
    #3
  4. Dear Repliers!

    It's the case I'm working on:
    <xsl:choose>
    <xsl:when test="viewheaderparameter_id = 32000">

    AddViewParameter(0,viwsecPageContent,hdrprmPersonelId"<xsl:value-of
    select="normalize-space(viewparameter_htmlcalssname)"/>", <xsl:value-of
    select="viewparameter_flags"/> );
    </xsl:when>
    <xsl:when test="viewheaderparameter_id = 32001">

    AddViewParameter(0,viwsecPageContent,hdrprmPersonelName"<xsl:value-of
    select="normalize-space(viewparameter_htmlcalssname)"/>", <xsl:value-of
    select="viewparameter_flags"/> );
    </xsl:when>
    <!--more when .... -->
    </xsl:choose>

    I'm genrating JScript code from an XML. Based on the value of
    "viewheaderparameter_id" I should generate "AddViewParameter" function
    with deifferent arguments. It's 3rd argument (hdrprmPersonelId,
    hdrprmPersonelName, ...) must change in each xsl:when.

    I don't want to repeat entire "AddViewParameter(..", instead to set a
    variable in each xsl:when and finally produce my desired function...


    Regards,
    Afshar Mohebbi



    Joe Kesselman wrote:
    > In XSLT, all variables are single-assignment. You can mask them with
    > another variable having the same name in a deeper scope, but when you
    > exit that scope you're looking at the original variable again. (This is
    > a direct consequence of XSLT being designed as a pure functional
    > language, which has implications for efficiency.)
    >
    > Yes, this requires that you change your coding style to fit the
    > language's capabilities.
    >
    > --
    > () ASCII Ribbon Campaign | Joe Kesselman
    > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Afshar Mohebbi, Jun 26, 2006
    #4
  5. Afshar Mohebbi

    Andy Dingley Guest

    > I don't want to repeat entire "AddViewParameter(..", instead to set a
    > variable in each xsl:when and finally produce my desired function...


    Try something like this:

    <xsl:variable name="foo" ><xsl:choose>
    <xsl:when test="viewheaderparameter_id = 32000">param1,
    param2</xsl:when>
    <xsl:when test="viewheaderparameter_id = 32001">param1, param2,
    param3</xsl:when>
    </xsl:choose></xsl:variable>

    AddViewParameter(0,viwsecPageContent,hdrprmPersonelId"<xsl:value-of
    select="normalize-space( $foo )"/>", <xsl:value-of
    select="viewparameter_flags"/> );

    You can set the value of an XSL variable as often as you like, to as
    many different values as you like. The trick is that each instance must
    be within a different scope (effectively the XML nesting level within
    your XSL stylesheet).

    Note here that I've embedded the <xsl:choose> within the <xsl:variable>
    This is because doing it the other way round would be a much more
    obvious way to set the variable, but it would also limit its scope so
    much that its value couldn't be accessed from outside the <xsl:choose>.
     
    Andy Dingley, Jun 26, 2006
    #5
  6. Hi Andy!

    That's a really nice trick! Thank you very much!

    Afshar


    Andy Dingley <> wrote:
    > > I don't want to repeat entire "AddViewParameter(..", instead to set a
    > > variable in each xsl:when and finally produce my desired function...

    >
    > Try something like this:
    >
    > <xsl:variable name="foo" ><xsl:choose>
    > <xsl:when test="viewheaderparameter_id = 32000">param1,
    > param2</xsl:when>
    > <xsl:when test="viewheaderparameter_id = 32001">param1, param2,
    > param3</xsl:when>
    > </xsl:choose></xsl:variable>
    >
    > AddViewParameter(0,viwsecPageContent,hdrprmPersonelId"<xsl:value-of
    > select="normalize-space( $foo )"/>", <xsl:value-of
    > select="viewparameter_flags"/> );
    >
    > You can set the value of an XSL variable as often as you like, to as
    > many different values as you like. The trick is that each instance must
    > be within a different scope (effectively the XML nesting level within
    > your XSL stylesheet).
    >
    > Note here that I've embedded the <xsl:choose> within the <xsl:variable>
    > This is because doing it the other way round would be a much more
    > obvious way to set the variable, but it would also limit its scope so
    > much that its value couldn't be accessed from outside the <xsl:choose>.
     
    Afshar Mohebbi, Jun 26, 2006
    #6
    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. Amir
    Replies:
    3
    Views:
    599
  2. nc
    Replies:
    1
    Views:
    502
    nice.guy.nige
    Feb 3, 2005
  3. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    2
    Views:
    339
  5. Jeannie
    Replies:
    15
    Views:
    893
    Jeannie
    Aug 30, 2005
Loading...

Share This Page