15

I'm working on an application which uses apache cocoon to convert an XML to PDF, and I'm redesigning the XSL that handles the input XML.

Currently in the XSL, we have code like this

<xsl:variable name="variable1">
   <xsl:choose>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'A'"/>
     </xsl:when>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'B'"/>
     </xsl:when>
   </xsl:choose>
</xsl:variable>

<xsl:variable name="variable2">
   <xsl:choose>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'X'"/>
     </xsl:when>
     <xsl:when test="$testVariable ='1'">
        <xsl:value-of select="'Y'"/>
     </xsl:when>
   </xsl:choose>
</xsl:variable>

Will it work if I change it to this?

<xsl:variable name="variable1"/>
<xsl:variable name="variable2"/>
<xsl:choose>
   <xsl:when test="$testVariable ='1'">
         <xsl:variable name="variable1" select="'A'">        
         <xsl:variable name="variable2" select="'X'">
   </xsl:when> 
   <xsl:when test="$testVariable ='2'">
         <xsl:variable name="variable1" select="'B'">        
         <xsl:variable name="variable2" select="'Y'">
   </xsl:when> 
</xsl:choose>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
Ravi Shenoy
  • 467
  • 1
  • 4
  • 14

1 Answers1

31

No, unlike in a lot of other languages, XSLT variables cannot change their values after they are created. You can however, avoid extraneous code with a technique like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:variable name="mapping">
    <item key="1" v1="A" v2="B" />
    <item key="2" v1="X" v2="Y" />
  </xsl:variable>
  <xsl:variable name="mappingNode"
                select="document('')//xsl:variable[@name = 'mapping']" />

  <xsl:template match="....">
    <xsl:variable name="testVariable" select="'1'" />

    <xsl:variable name="values" select="$mappingNode/item[@key = $testVariable]" />

    <xsl:variable name="variable1" select="$values/@v1" />
    <xsl:variable name="variable2" select="$values/@v2" />
  </xsl:template>
</xsl:stylesheet>

In fact, once you've got the values variable, you may not even need separate variable1 and variable2 variables. You could just use $values/@v1 and $values/@v2 instead.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thank you JLRishe. The example I have written above is simple representation of what is there. Actually, there are a lot of variables and lot of nesting involved. I'm looking for a way to optimize it – Ravi Shenoy Jan 30 '13 at 11:49
  • @RaviShenoy I'm not sure if that means I sufficiently answered your question or not. :) If there are additional details that need to be accounted for, please add those details to your question (and leave a comment here on my answer to let me know) and I will try to incorporate them into my answer. – JLRishe Jan 30 '13 at 12:01
  • But would @RaviShenoy 's code work if he removes the declarations of the variables at the beginning? – Anurag Jun 18 '13 at 14:30
  • 2
    @Anurag No it probably wouldn't. The variables would only have scope inside the `` elements that contain them, and I'm pretty sure that's not where he wants to use them. – JLRishe Jun 18 '13 at 17:21