By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

xsl:variable is Constant or really Variable?

P: n/a
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

Jun 25 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a


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/
Jun 25 '06 #2

P: n/a
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
Jun 25 '06 #3

P: n/a
Dear Repliers!

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

AddViewParameter(0,viwsecPageContent,hdrprmPers onelId"<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,hdrprmPers onelName"<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


Jun 26 '06 #4

P: n/a
> 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,hdrprmPers onelId"<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>.

Jun 26 '06 #5

P: n/a
Hi Andy!

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

Afshar
Andy Dingley <di*****@codesmiths.com> 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,hdrprmPers onelId"<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>.


Jun 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.