XSL and variables

I’ve been working with XSL lately, and been really impressed with the power of XSL and transformations. One thing I really like is how variables are declared within an XSL stylesheet.

For simple variables, the declaration looks like this:

   <xsl:variable name=”aVariable” select=”‘someValue'”/>

Variable declarations do not stop there. What I really like is how complex variable declarations encapsulate the logic to assign their value. Consider the following example:

   <xsl:variable name=”aComplexExample”>
      <xsl:choose>
         <xsl:when test=”blah = blahblah”>
            first value
         </xsl:when>
         <xsl:when test=”blah2 = blahblahblah”>
            second value
         </xsl:when>
         <xsl:otherwise>
            default value
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>

Notice how the variable declaration wraps the logic in one nice package. Really nice.

However, there is one caveat with variables. They cannot be changed once they are assigned. So, if you want to increment a counter, you’re out of luck. In my case, I was fortunate enough that I could change the XML file definition so I could get the desired results. So really the name variable should be constant instead.

One last thought. When you have control of the layout with XML and XSL files, work backwards by creating the desired output first, changing the XML format as necessary.

This entry was posted in Uncategorized. Bookmark the permalink.