[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Variable value based on condition[URGENT]

Subject: Re: Variable value based on condition[URGENT]
From: Mike Brown <mike@xxxxxxxx>
Date: Sun, 2 Dec 2001 11:05:36 -0700 (MST)
xslt variable condition
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.