This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Variable value based on condition[URGENT]


Mailer Mailer wrote:
> XSL does not allow variable reassignment. I want to
> assign a value to a variable based on a condition.

This is a FAQ, and you merely need to restate the problem.

In a procedural language you think of the problem as a choice of variable
assignments based on conditions:

   if (a):
     x = z
   elif (b):
     x = zz
   else:
     x = zzz

But in a declarative, functional language like XSLT, you must think of the
problem as a single variable assignment where the value is chosen based on
conditions:

   x = {
     if (a):
       z
     elif (b):
       zz
     else:
       zzz
   }

The actual code in XSLT would be as follows:

   <xsl:variable name="x">
     <xsl:choose>
       <xsl:when test="a">z</xsl:when>
       <xsl:when test="b">zz</xsl:when>
       <xsl:otherwise>zzz</xsl:otherwise>
     </xsl:choose>
   </xsl:variable>

One could argue that this is simpler and more efficient for an XSLT engine
to process, and is therefore more elegant, although it may not have been
the natural solution for you.

The only issue you need to be aware of with this example is that the data
type of $x is now a result tree fragment; if you needed it to be a
node-set, you would have to assign a separate variable that does the
conversion. Other data types usually don't need such conversion.

   - Mike
____________________________________________________________________________
  mike j. brown, fourthought.com  |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  personal: http://hyperreal.org/~mike/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]