2010-02-13 40 views
9

在我的XSLT电子表格中,我需要根据xml节点的值定义一个或另一个值的xsl:变量。下面的代码显示了我想要做的事情。我想用这种方式定义多个变量。使用xsl动态定义XSLT变量:选择

一个主要的问题是,为了根据每个项目的节点值选择一个变量值,选择必须在xsl:foreach内完成,并且每当我尝试在xsl中定义一个变量时:foreach它显示一个错误。

<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}"> 

我想通过改变变量值来完成此的原因是因为这些变量然后在数学函数,它由节点值(价格),其将与每个不同乘以变量使用。以下是如何使用一旦定义的变量。非常感谢。

<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: 确定。我已经尝试过Dark Falcon提供的解决方案,但它一直给我一个错误“开放和结束标签不匹配”。与以前一样的错误。它似乎不喜欢有xsl:选择我拥有它的地方,因为那些行号是错误来自的地方。这里是所有相关的样式代码:

<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> 

回答

0

这可能不是最好的办法,但:

我会建议您因素的逻辑出显示价格进入一个模板,然后用

<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> 
+0

我尝试使用调用模板函数的建议,但我得到了同样的错误。我已经更新了我的问题。谢谢! – Alan 2010-02-14 00:11:39

+0

对不起,with-param需要是空标签。现在它没有结束标签。 – 2010-02-14 00:30:04

11

我认为唯一不对您最初的代码基本上如下:

<xsl:variable name="rent1" select="number($rate1)"> 

(无{},因为它select,你可能想在该变量的数字,而不是字符串。)

因此,这将是这样的:

<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)"> 
+0

呼叫号码()。我将发布最终的代码。 – Alan 2010-02-15 00:46:52

2

明白了。这是结束工作的代码。该解决方案是使用“number()”并直接调用变量而不是首先定义它的组合。谢谢大家。

<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> 
0

试试这个:

<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>