tags:

views:

24

answers:

2

I call a template like this:

<xsl:call-template name="trip_form">
   <xsl:with-param name="header" select="'Spara din resa'" />
   <xsl:with-param name="size" select="'savetrip'" />
  </xsl:call-template>

The templte looks like this:

<xsl:template name="trip_form">
  <xsl:param name="type" />
  <xsl:param name="size" />
  <xsl:param name="header" />
  <xsl:if test="type = ''">
   <xsl:if test="/root/meta/url_params/has_car = 1">
    <xsl:with-param name="'type'" select="'driver'" />
   </xsl:if>
   <xsl:if test="/root/meta/url_params/has_car = 0">
    <xsl:variable name="'type'" select="'passenger'" />
   </xsl:if>
  </xsl:if>

etc...

When I specify the $type variable whilst calling the template, I want to use that value, but when I don't I want to check the Url_params/has_car node and set the variable accordingly, how do I do this?

+2  A: 

You can't update the value of a parameter or variable after it has been initially set. However, what you could do, is create a new variable, and set that according to whether the original parameter $type has been set or not.

Try something like this. This creates a new variable, $newtype, which you can then use in your template. If $type is set, then $newtype will equal $type, otherwise it will look at the Url_params/has_car elements

<xsl:template name="trip_form">
   <xsl:param name="type"/>
   <xsl:param name="size"/>
   <xsl:param name="header"/>
   <xsl:variable name="newtype">
      <xsl:choose>
         <!-- Check if type is defined -->
         <xsl:when test="$type = ''">
            <!-- Determine if a driver or passenger -->
            <xsl:choose>
               <xsl:when test="/root/meta/url_params/has_car = 1">
                  <xsl:value-of select="'driver'"/>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:value-of select="'passenger'"/>
               </xsl:otherwise>
            </xsl:choose>
         </xsl:when>
         <xsl:otherwise>
            <!-- Use value of parameter -->
            <xsl:value-of select="$type"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>

   <!-- Use the new variable $newtype here -->
   <xsl:value-of select="$newtype"/>
</xsl:template>
Tim C
this is grate, I also like when/otherwise, I have been looking for something like that!
Kristoffer Nolgren
A: 

If you want to define a default value for a param, you just need to add it to the param declaration. As example:

<xsl:template name="trip_form"> 
    <xsl:param name="type"> 
        <xsl:choose> 
           <xsl:when test="/root/meta/url_params/has_car = 1">driver</xsl:when> 
           <xsl:otherwise>passenger</xsl:otherwise> 
        </xsl:choose> 
    </xsl:param> 
    <xsl:param name="size"/> 
    <xsl:param name="header"/> 
</xsl:template> 

Or:

<xsl:template name="trip_form"> 
    <xsl:param name="type" 
               select="substring('passengerdriver',
                                 1 + 9 * (/root/meta/url_params/has_car = 1),
                                 9 - 3 * (/root/meta/url_params/has_car = 1)"> 
    </xsl:param> 
    <xsl:param name="size"/> 
    <xsl:param name="header"/> 
</xsl:template> 
Alejandro