[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] Variable value based on condition[URGENT]
Subject: Re: [xsl] Variable value based on condition[URGENT] From: Mike Brown <mike@xxxxxxxx> Date: Sun, 2 Dec 2001 11:05:36 -0700 (MST) |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Variable value based on c, Mailer Mailer | Thread | [xsl] CDATA required for javascript, yan bai |
RE: [xsl] Variable value based on c, Chris Bayes | Date | RE: [xsl] Rendering HTML via XSLT w, Mulberry Technologie |
Month |