Question

I have a piece of code that look similar to this:

<xsl:choose>
   <xsl:when test="some_test">   
      <xsl:value-of select="Something" />
      You are:
      <xsl:variable name="age">12</xsl:variable>
      years
   </xsl:when>
</xsl:choose>

My problem is that I would like to use the variable $age outside of the choose. How do I do that?

A similar problem is that I have several templates in my XSLT-file, and one of them is the main template. This one:

<xsl:template match="/">
</xsl:template>

Inside of this template, I call several other templates, and again I would like to use some of the variables from the other templates.

In example, if I have this code:

<xsl:template match="/">
   <xsl:call-template name="search">
   </xsl:call-template>
</xsl:template>

<xsl:template name="search">
   <xsl:variable name="searchVar">Something...</xsl:variable>
</xsl:template>

Then I would like to use the $searchVar inside of my main-template.

It's kind of the same issue I think, but I can't seem to figure this one out.

And I run Umbraco as my CMS by the way :)

I hope that some of you have the answer.

Thanks - Kim

Was it helpful?

Solution

To #1: Variables are valid within their parent element only. Which means you must put the logic inside the variable instead of "around" it:

<xsl:variable name="var">
  <xsl:choose>
    <xsl:when test="some_test">
      <xsl:text>HEY!</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>SEE YA!</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

To #2: Use parameters to transport values into templates.

<xsl:template match="/">
  <xsl:variable name="var" select="'something'" />

  <!-- params work for named templates.. -->
  <xsl:call-template name="search">
    <xsl:with-param name="p" select="$var" />
  </xsl:call-template>

  <!-- ...and for normal templates as well -->
  <xsl:apply-templates select="xpath/to/nodes">
    <xsl:with-param name="p" select="$var" />
  </xsl:apply-templates>
</xsl:template>

<!-- named template -->
<xsl:template name="search">
  <xsl:param name="p" />

  <!-- does stuff with $p -->
</xsl:template>

<-- normal template -->
<xsl:template match="nodes">
  <xsl:param name="p" />

  <!-- does stuff with $p -->
</xsl:template>

To transport a value back to the calling template, combine the above:

<xsl:template match="/">
  <xsl:variable name="var">
    <xsl:call-template name="age">
      <xsl:with-param name="num" select="28" />
    </xsl:call-template>
  </xsl:variable>

  <xsl:value-of select="$var" />
</xsl:template>

<xsl:template name="age">
  <xsl:param name="num" />

  <xsl:value-of select="concat('You are ', $num, ' years yold!')" />
</xsl:template>

OTHER TIPS

Have a look at xsl:param.

Edit: Untested, but may work:

<xsl:param name="var">
  <xsl:choose>
   <xsl:when test="some_test">   
     <xsl:value-of select="string('HEY!')" />
   </xsl:when>
  </xsl:choose>
</xsl:param>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top