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: how to reassign a variable's value?


>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@onebox.com - email
 

 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]