title: XSLT: Pass variable into template when using apply-templates
body: <p>I'm not sure if this is asking too much of XSLT, but I'm after something similar to how <code>mode</code> works, except ideally within one template, rather than having to duplicate it as I am now:</p>
  
  <pre><code>&lt;!-- Start --&gt;
  &lt;xsl:apply-templates select="airports/airport" mode="start" /&gt;
  
  &lt;!-- End --&gt;
  &lt;xsl:apply-templates select="airports/airport" mode="end" /&gt;
  
  &lt;!-- Template --&gt;
  &lt;xsl:template match="airports/airport" mode="start"&gt;
      &lt;option value="{@iata}" data-alternative-spellings="{.},{@iata}"&gt;
          &lt;xsl:if test="@iata = 'LGW'"&gt;
              &lt;xsl:attribute name="selected"&gt;selected&lt;/xsl:attribute&gt;
          &lt;/xsl:if&gt;
          &lt;xsl:value-of select="@iata"/&gt; - &lt;xsl:value-of select="."/&gt;
      &lt;/option&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="airports/airport" mode="end"&gt;
      &lt;option value="{@iata}" data-alternative-spellings="{.},{@iata}"&gt;
          &lt;xsl:if test="@iata = 'LAX'"&gt;
              &lt;xsl:attribute name="selected"&gt;selected&lt;/xsl:attribute&gt;
          &lt;/xsl:if&gt;
          &lt;xsl:value-of select="@iata"/&gt; - &lt;xsl:value-of select="."/&gt;
      &lt;/option&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>The template applies the <code>selected</code> attribute if the value is something specific. This value needs to be different depending on what's calling the template. There are two cases, <code>start</code> and <code>end</code> that both have different criteria.</p>
  
  <p>The easiest way to explain what I'm attempting to do is for me to write it in another language :)</p>
  
  <pre><code>&lt;!-- Start --&gt;
  getAirports(start);
  
  &lt;!-- End --&gt;
  getAirports(end);
  
  &lt;!-- Template --&gt;
  var getAirports = function(position)
  {
      var selected = '';
      switch(position)
      {
          case 'start':
              if(iata == 'LGW')
              {
                  var selected = 'selected="selected"';
              }
          break;
          case 'end':
              if(iata == 'LAX')
              {
                  var selected = 'selected="selected"';
              }
          break;
          default:
          break;
      }
      return '&lt;option value="'+iata+'" data-alternative-spellings="'+iata+','+name+'" '+selected+'&gt;'+iata+' - '+name+'&lt;/option&gt;';
  }
  </code></pre>
  
  <p>Is this possible in xslt, or will I have to stick to duplicating the template and using <code>mode</code>?</p>
  
  <p>Thanks!</p>