Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining XSLT Variables dynamically using xsl:choose

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>
like image 813
Alan Avatar asked Feb 13 '10 22:02

Alan


People also ask

How to define variable in xsl?

The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

How do I change the value of a variable in XSLT?

You cannot - 'variables' in XSLT are actually more like constants in other languages, they cannot change value. Save this answer.

How to add variables in XSLT?

You use the <xsl:variable> element to declare a variable and assign a value to it. Remember that these two must always be done at the same time in XSLT, you can't declare a variable and later assign it a value.

What is difference between Param and variable in XSLT?

The content model of both elements is the same. The way these elements declare variables is the same. However, the value of the variable declared using <xsl:param> is only a default that can be changed with the <xsl:with-param> element, while the <xsl:variable> value cannot be changed.


2 Answers

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)">
like image 60
Lucero Avatar answered Sep 17 '22 20:09

Lucero


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>
like image 37
Alan Avatar answered Sep 17 '22 20:09

Alan