tags:

views:

36

answers:

1

Hi folks,

I need your help in the following scenario :

<xsl:variable name="var1" select="'SOME_DATA1'" />
<xsl:if test="'some_condition'">
                <xsl:variable name="var2" >
                    <xsl:value-of select="'SOME_DATA2'"/>
                </xsl:variable>
</xsl:if>
<data> <!-- I need here to contact var1 with var2, please help --> </data>

Thanks.

+3  A: 

How about:

<data>
  <xsl:variable name="var1" select="'SOME_DATA1'" />
  <xsl:text><xsl:value-of select="var1"/></xsl:text>
  <xsl:if test="'some_condition'">
     <xsl:variable name="var2" >
        <xsl:value-of select="'SOME_DATA2'"/>
     </xsl:variable>
     <xsl:text><xsl:value-of select="var2"/></xsl:text>
  </xsl:if>
</data>

The way you wrote it, var2 does not exist after the /xsl:if.

Another way would be like this:

<xsl:variable name="var1" select="'SOME_DATA1'" />
<xsl:variable name="var2" >
  <xsl:if test="'some_condition'">
     <xsl:value-of select="'SOME_DATA2'"/>
  </xsl:if>
</xsl:variable>
<data> <!-- Use var1 and var2 here --> </data>

In this way, you have a var2 regarardeless of the condition, but it is empty if the condition is false. And you still have the variable after the condition.

Rasmus Kaj
+1 for way #2. Your first proposal is kind of horrible.
Tomalak
I think #1 don't allow us to put empty data as #2 do.
Mohammed
I admit there's not much point in using variables at all in #1 ... But if the `<data>` element is the only thing the variables is for, I think #1 without the actual variables (i.e. just put the expressions in the xsl:text/xsl:value-of) is the way to go.
Rasmus Kaj