Re: [xsl] Using variables or parameters in tests

Subject: Re: [xsl] Using variables or parameters in tests
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 1 Mar 2005 23:33:19 GMT
firstly, obligatory daily comment on this list:-)

don't do this:

  <xsl:variable name="actualelectricity">		<xsl:value-of 
  select="sum(/statement/item[$datetest and $electricitytest]/amount)"/>

do this

<xsl:variable name="actualelectricity"
select="sum(/statement/item[$datetest and $electricitytest]/amount)"/>

as it's less to type and loads more efficient as the variable then holds
a number not a result tree fragment with a root node and a text node with
string value the decimal representation of a number.

However that doesn't really address your question...

  datetest = (date >= 20050228)

variables hold values not expressions, so you while this

select="sum(/statement/item[$datetest and $electricitytest]/amount)"/>

is legal; $datetest would be a constant holding whatever value it is
bound to, it wouldn't get a new value relative to each item node.

in xslt1 if you want to store an expression that is going to be
re-evaluated you need to put it in a named template. Then you could call
that so long as you replaced sum(..) with a recursive template that
iterated over the nodes calculating the sum and calling your named

in xslt2 draft you could define datetest as a function something like
<xsl:function name="my:datetest" as="xs:boolean">
 <xsl:param name="here"/>
 <xsl:sequence select="$here/date &gt;= 20005028"/>

and similarly your other test then

select="sum(/statement/item[my:datetest(.) and my:electricitytest(.)]/amount)"/>


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:

Current Thread