Re: [xsl] how to reassign a variable's value?

Subject: Re: [xsl] how to reassign a variable's value?
From: "Charles Knell" <cknell@xxxxxxxxxx>
Date: Mon, 12 Aug 2002 13:56:10 -0700
>You can't change the value of a variable in XSLT [...].
> 
> This must be one of the most frequently asked questions, and for a
> good
> reason. Haven't anyone ever proposed renaming <xsl:variable> to
> <xsl:constant>? To me, the essence of a "variable" is that it can change
> its value. This deserves to be one of the most frequently proposals.


The value of an XSLT variable can, and often does, change. Just not in
the same context where it was defined. A variable gets to have a value
assigned exatcly once in a given context. When the context node changes,
a new value will be assigned.

If you have a data file like this:

<root>
  <house>
    <location></location>
    <size></size>
    <value></value>
  </house>
  <house>
    <location></location>
    <size></size>
    <value></value>
  </house>
  <house>
    <location></location>
    <size></size>
    <value></value>
  </house>
</root>

and a template in your XSLT file like this:

<template match="house">
  <xsl:variable name="unitPrice" select="value div size" />
  <td><xsl:value-of select="location" /></td><td><xsl:value-of select="$unitPrice"
/></td>
</template>

every time that template is matched, the value of $unitPrice will vary,
so that it is no more a variable than it is a constant. It is constant
within a context and variable between contexts. "Varinstant", perhaps?
-- 
Charles Knell
cknell@xxxxxxxxxx - email
 

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


Current Thread