Changing value of an XSLT variable (don't laugh!)

| 8 Comments | No TrackBacks

Can one change a value of a variable in XSLT? Unexpected answer - yes, when debugging XSLT in Visual Studio 2005 Beta2. I'm not sure even if it's a bug. Actually it can be quite useful when debugging to be able to change some values, right? Do other XSLT debuggers allow it?

I'm learning new XML stuff in Visual Studio 2005 Beta2. So far I'm still overwhelmed! New System.Xml is sooooo cool, elegant and professional, kudos to the WebData team, this is really a piece of programming art. Still I filed 4 bugs so far in just one hour.

Related Blog Posts

No TrackBacks

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/433

8 Comments

You can't update variable, once the value of a variable is declared in a specific scope, you can use a recursive
call.

hey man i tried to produce an alternate coloured table using XSL but couldnt achive it using the variable change.....unforunately my VS2005 is not allowing shadowing of the variable....says another scope of same variable exists :( here's my code lisiting....XSL
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<style type="text/CSS">
body {color:red;}
table tr.green {border:0;background-color:green;}
table tr.yellow {border:0;background-color:yellow;}
</style>
<html>
<body>
<table>
<xsl:variable name="counter" select="1"/>
<xsl:for-each select="ResumeInfo/Resume">
<xsl:element name="TR">
<xsl:if test="$counter=1">
<xsl:attribute name="class">
<xsl:value-of select="'yellow'"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$counter!=2">
<xsl:attribute name="class">
<xsl:value-of select="'green'"/>
</xsl:attribute>
</xsl:if>
<xsl:element name="TD">
<xsl:value-of select="@ID"/>
</xsl:element>
<xsl:element name="TD">
<xsl:value-of select="@Date"/>
</xsl:element>
<xsl:element name="TD">
<xsl:value-of select="Heading"/>
</xsl:element>
<xsl:for-each select="Anchor">
<xsl:element name="TD">
<xsl:value-of select="@Link"/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="IMG">
<xsl:element name="TD">
<xsl:value-of select="@SRC"/>
</xsl:element>
</xsl:for-each>
<xsl:if test="$counter=1">
<xsl:variable name="counter" select="0"/>
</xsl:if>
</xsl:element>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Hi..,
Your Code is very useful to me.., i want to use the variable value in another template . if i use ur method in another template it shows the value which is declared at the first.
iam new to XSLT it will be helpful to me if u solve my problem.
Thanks and Regards
AjayKumar.J

Well, these are fairly reasonable limitations I think.

1) There are no bugs in our product, only undocumented features :)
2) If the spec prohibits changing a value of a variable, don't do that :)
3) The actual problem is that changing a type of a variable or changing a value of a variable whose type is node-set is not supported.

Anton, I know, XslTransform's full of such surprises. That's one of the cases I tested with XslCompiledTransform the first day I installed Beta2.

Maybe you'll laugh, but it IS possible to change the value of a variable with XslTransform:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:variable name="x" select="0" />
<xsl:template match="/">
<xsl:for-each select="//*">
x = <xsl:variable name="x" select="$x+1" /><xsl:value-of select="$x" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Changing the value of a variable with the help of the debugger will be useful only if the XSLT processor is guaranteed not to re-evaluate the variable.

Leave a comment