tags:

views:

365

answers:

2

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

A: 

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>
Scoregraphic
I can't use the <xsl:with-param> inside of a choose...
Kim Andersen
I edited my reply
Scoregraphic
Hmm...yeah that might work, but actually I have more than my variable in the choose. I also have some plain text and other variables.So if I do it this way, the variable called $var, would contain all of that information, and that's not the meaning.
Kim Andersen
Then you are doing it wrong. ;-) You want two variables, then you have to declare two variables. This could mean you implement more or less the same logic twice, but that's the only way. In your attempt, both variables go out of scope right at the closing `</xsl:when>`.
Tomalak
@Scoregraphic: 'HEY!' is a a string already. `<xsl:value-of select="'HEY!'" />` suffices. ;-) Better yet: `<xsl:text>HEY!</xsl:text>`.
Tomalak
@Tomalak...yes, but i wanted to make sure he can use there "everything" as HEY! is only a simplified substitute here ;-)
Scoregraphic
+1  A: 

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>
Tomalak
Hi there Tomalak#1:See my last comment to Scoregraphic. Just made a better example in the question.#2:The problem is not to use a variable from the main-template in the other templates. But to use a variable whos defined in the other templates in the main-template.
Kim Andersen
#1: See my other comment. BTW, your updated example #1 does not make much sense. Posting *actual* code is better than making up stuff that does not resemble your real problem. #2: See my edited answer.
Tomalak