xsl:variable is Constant or really Variable?

A

Afshar Mohebbi

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
 
M

Martin Honnen

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?
 
J

Joe Kesselman

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

Afshar Mohebbi

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
 
A

Andy Dingley

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top