使用xsl:choose动态定义XSLT变量 [英] Defining XSLT Variables dynamically using xsl:choose

查看:67
本文介绍了使用xsl:choose动态定义XSLT变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的XSLT电子表格中,我需要定义一个xsl:variable,它具有一个或另一个值,具体取决于xml节点的值.下面的代码显示了我正在尝试执行的操作.我想用这种方式定义多个变量.

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.

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

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>

更新:好的.我已经尝试过Dark Falcon下面提供的解决方案,但是它一直给我一个错误打开和结束标签不匹配".与以前相同的错误.看起来好像不喜欢xsl:choose,因为这些行号是错误的来源.这是所有相关的样式表代码:

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>

推荐答案

我认为您的初始代码唯一的错误基本上是以下内容:

I think the only thing wrong with your initial code is basically the following:

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

(没有 {} ,因为它是 select ,并且您可能希望在该变量中使用数字而不是字符串.)

(No {} because it it a select and you probably want to have a number in that variable, not a string.)

那将是这样的:

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

这篇关于使用xsl:choose动态定义XSLT变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆