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. Advertisements

  2. Afshar Mohebbi wrote:

    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, Jun 25, 2006
    #2
    1. Advertisements

  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.
     
    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
     
    Afshar Mohebbi, Jun 26, 2006
    #4
  5. Afshar Mohebbi

    Andy Dingley Guest

    I don't want to repeat entire "AddViewParameter(..", instead to set a
    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
     
    Afshar Mohebbi, Jun 26, 2006
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.