tags:

views:

397

answers:

3

Is it possible to add text from a xsl variable to the inside of a html tag something like this?

<xsl:variable name="selected">
   <xsl:if test="@class = 'selected'"> class="selected"</xsl:if>
</xsl:variable>

<li{$selected}></li>
+2  A: 

You should not attempt to write this as literal text, instead look at xsl:element and xsl:attribute. Rough example:

<xsl:element name="li">
    <xsl:attribute name="class">
        <xsl:value-of select="$selected" />
    </xsl:attribute>
</xsl:element>

Full documentation here.

apathetic
+3  A: 

Try this:

<xsl:element name="li">
    <xsl:if test="@class = 'selected'">
     <xsl:attribute name="class">
      selected
     </xsl:attribute>
    </xsl:if>
</xsl:element>

Optionally, the xsl:if could be nested in the xsl:attribute, instead of the other way around, if a class="" is desired. As already mentioned, it is unwise to write this as literal text.

Eric
For readability, it's best to avoid unnecessary xsl:element calls and embed the li element directly.
Eamon Nerbonne
+2  A: 

Note that if you are careful enough to make it its first child, you can use directly inside of your <li> tag, instead of using <xsl:element>

<li>
    <xsl:if test="$selected">
     <!-- Will refer to the last opened element, li -->
     <xsl:attribute name="class">selected</xsl:attribute>
    </xsl:if>

    <!-- Anything else must come _after_ xsl:attribute -->
</li>
Josh Davis