Zack Macomber
Zack Macomber

Reputation: 6905

xslt variable set based on condition

I would like to set an xsl variable based on a condition. The xsl below is not currently working for me. I've been able to make two different matchers (<xsl:template match="rule/condition" and <xsl:template match="condition/condition") which enables me to put the ;display:none on just condition/condition matches but that results in the template being duplicated except for the one part of ;display:none. Guess I'm under the impression that I should be able to dynamically set a variable based on a condition but maybe my impression is wrong?

<xsl:template match="condition">
    <xsl:variable name="display">
        <xsl:if test='name(..)=condition'>;display=none</xsl:if>
    </xsl:variable>
    <div style="{$divIndent}{$display}">
        <a href="javascript:void(0)" style="{$expandPosition}" onclick="expandContents(this)">
            <span class="glyphicon glyphicon-plus"></span>
        </a>
        &lt;condition type="<xsl:value-of select="@type"/>"&gt;<br />
        <xsl:apply-templates select="expression" />
        <xsl:apply-templates select="condition" />
        &lt;/condition&gt;
    </div>
</xsl:template>

Upvotes: 1

Views: 492

Answers (2)

Ankit Rathore
Ankit Rathore

Reputation: 46

Try below code by adding attribute style

<div><xsl:attribute name ="style"><xsl:if test='name(..)=condition'>display=none;</xsl:if></xsl:attribute></div>

Upvotes: 0

user52889
user52889

Reputation: 1501

<xsl:if test='name(..)=condition'>;display=none</xsl:if>

This asks if the name of the parent is equal to the value of the child whose name is condition. You probably want to know if the name of the parent is the literal value "condition":

<xsl:if test='name(..)="condition"'>;display=none</xsl:if>

However, it might be more idiomatic to write:

<xsl:if test='parent::condition'>;display=none</xsl:if>

Note also that display:none is valid css, display=none prbably won't work.

Upvotes: 2

Related Questions