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>
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>
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>
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.
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>