问题
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>
回答1:
I think the only thing wrong with your initial code is basically the following:
<xsl:variable name="rent1" select="number($rate1)">
(No {}
because it it a select
and you probably want to have a number in that variable, not a string.)
So that would be something like this:
<xsl:variable name="rate1">
<xsl:choose>
<xsl:when test="rental='new'">0.15</xsl:when>
<xsl:otherwise>0.30</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="rent1" select="number($rate1)">
回答2:
Got it. Here's the code that ended up working. The solution was a combination of using "number( )" and calling the variable directly instead of defining it first. Thanks all.
<xsl:variable name="rate">
<xsl:choose>
<xsl:when test="rental='new'">
<xsl:value-of select="'.15'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'.30'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<div class="rental-price"><em>$ <xsl:value-of select='format-number( (100*(price * number($rate)) div 100), "###.00" )'/></em></div>
回答3:
This may not be the best way but:
I would recommend you factor your logic for displaying the price out into a template, then use
<xsl:choose>
<xsl:when test="rental='new'">
<xsl:call-template name="showPrice">
<xsl:with-param name="rent" select="'.15'" />
</xsl:call-template>
</xsl:when>
<xsl:when test="rental='used'">
<xsl:call-template name="showPrice">
<xsl:with-param name="rent" select="'.30'" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
回答4:
Try this:
<xsl:variable name="rate">
<xsl:if test="rental='new'">.15</xsl:if>
<xsl:if test="rental='used'">.30</xsl:if>
</xsl:variable>
<xsl:call-template name="showPrice">
<xsl:with-param name="rent">
<xsl:value-of select="$rate"/>
</xsl:with-param>
</xsl:call-template>
来源:https://stackoverflow.com/questions/2259322/defining-xslt-variables-dynamically-using-xslchoose