RE: [xsl] Adding Variables

Subject: RE: [xsl] Adding Variables
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Mon, 28 Jun 2004 12:43:41 +0100
> Hi All,
> I've created a number of xsl variables in my template
> and I now wish to add these variables together and
> render the result in my template. For example,
> 
> <xsl:variable name="num1">
> <xsl:value-of select="//test1/somevalue1"/>
> </xsl:variable>
> 
> and
> 
> <xsl:variable name="num2">
> <xsl:value-of select="//test2/somevalue2"/>
> </xsl:variable>
> 
> Trying <xsl:value-of select="$num1+$num2"/> doesn't
> work.
> 

What do you mean by "it doesn't work"? There's nothing wrong with your code,
though it will produce NaN if the variables aren't numeric.

However, try to get out of the habit of:

<xsl:variable name="num1">
  <xsl:value-of select="//test1/somevalue1"/>
</xsl:variable>

It's an absurdly long-winded and inefficient way of doing:

<xsl:variable name="num1" select="//test1/somevalue1"/>

(The compiler can't automatically convert one to the other, because some
expressions treat them differently, for example boolean($num1) is always
true in the first case, but in the second case it is true only if
//test1/somevalue1 selects at least one node).

Michael Kay


Current Thread