conditional variable assignment

Subject: conditional variable assignment
From: Mike Brown <mbrown@xxxxxxxxxxxxx>
Date: Tue, 18 May 1999 10:46:47 -0600
I'm having to unlearn everything I take for granted in Perl when dealing
with XSL. Take conditional variable assignments, like "if x, then a=b". Easy
enough to do in XSL:

<xsl:if test="some_expr">
    <xsl:variable name="a" expr="b">

Easy, that is, if you don't mind the fact that your variable only exists
within the <xsl:if> node and its children. So, the following won't work:

<xsl:variable name="color" expr="Get/Color/From/Some/@Attribute">
    <xsl:when test="$color='red'">
        <xsl:variable name="tint" expr="'pink'">
        <xsl:variable name="shade" expr="'maroon'">
    <xsl:when test="$color='violet">
        <xsl:variable name="tint" expr="'lavender'">
        <xsl:variable name="shade" expr="'indigo'">
        <xsl:variable name="tint" expr="'light gray'">
        <xsl:variable name="shade" expr="'dark gray'">
The color is <xsl:value-of select="$color"/>...
The tint is <xsl:value-of select="$tint"/> (not there, is it?)...
The shade is <xsl:value-of select="$shade"/> (not there either!)

So, then, is the solution to be found in some less than obvious hierarchy of
XSL instructions, or is an entirely different approach necessary?

 XSL-List info and archive:

Current Thread