Within my XSLT spreadsheet, I need to define an xsl:variable with one value or another depending on the value of an xml node. The code just below shows what I'm trying to do. I would like to define multiple variables this way.
A major issue is that in order to choose a variable value based on the node value of each item, the choosing must be done within xsl:foreach, and whenever I try to define a variable within xsl:foreach it shows an error.
<xsl:for-each select="WORKS/item">
<xsl:variable name="rate1">
<xsl:choose>
<xsl:when test="rental='new'">
<xsl:value-of select="'.15'" />
</xsl:when>
<xsl:when test="rental='used'">
<xsl:value-of select="'.30'" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="rent1" select="{$rate1}">
The reason I'd like to accomplish this through changing the variable values is because those variables are then used in a math function, which multiplies the variable by a node value (price) which will be different with every . Here is how the variables, once defined, will be used. Thank you much.
<div class="rental-period">1-4 Days:</div>
<div class="rental-price"><em>$ <xsl:value-of select='format-number( (100*(price * $rent1) div 100), "###.00" )'/></em></div>
<div class="rental-period">5-7 Days:</div>
<div class="rental-price"><em>$ <xsl:value-of select='format-number( (100*(price * $rent2) div 100), "###.00" )'/></em></div>
<div class="rental-period">8-14 Days:</div>
<div class="rental-price"><em>$ <xsl:value-of select='format-number( (100*(price * $rent3) div 100), "###.00" )'/></em></div>
UPDATE: Ok. I've tried the solution provided below by Dark Falcon, but it keeps giving me an error "Opening and Ending Tags mismatch". The same error as before. It doesn't seem to like having the xsl:choose where I have it, since those line numbers are where the errors are coming from. Here is all the relevant stylesheet code:
<xsl:template name="showPrice">
<xsl:param name="rentalRate"/>
<div class="rental-price"><em>$ <xsl:value-of select='format-number( (100*(price * $rentalRate) div 100), "###.00" )'/></em></div>
</xsl:template>
<xsl:template match="/">
<xsl:for-each select="WORKS/item">
<div class="rental-info">
<xsl:choose>
<xsl:when test="rental='new'">
<xsl:call-template name="showPrice">
<xsl:with-param name="rentalRate" select="'.15'">
</xsl:call-template>
</xsl:when>
<xsl:when test="rental='used'">
<xsl:call-template name="showPrice">
<xsl:with-param name="rentalRate" select="'.30'">
</xsl:call-template>
</xsl:when>
</xsl:choose>
</div>
</xsl:for-each>
</xsl:template>