Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: If check parameter value

Tags:

xslt

Following code in xslt (I've cut out the irrelevant parts, get-textblock is a lot longer and has a lot of parameters which are all passed correctly):

<xsl:template name="get-textblock">
        <xsl:param name="style"/>

        <xsl:element name="Border">         
            <xsl:if test="$style='{StaticResource LabelText}'" >
                <xsl:attribute name="Background">#FF3B596E</xsl:attribute>
            </xsl:if>
            <xsl:attribute name="Background">#FF3B5940</xsl:attribute>              
        </xsl:element>

</xsl:template>

The style parameter can be either '{StaticResource LabelText}' or '{StaticResource ValueText}' and the background of the border depends on that value.

This if structure fails however, it always draws the FF3B5940 border in my output.xaml file. I call the template like this:

<xsl:call-template name="get-textblock">
    <xsl:with-param name="style">{StaticResource LabelText}</xsl:with-param>                    
</xsl:call-template>

Anyone sees what might be the problem? Thanks.

like image 930
Matthias Avatar asked Dec 12 '22 13:12

Matthias


2 Answers

The line:

<xsl:attribute name="Background">#FF3B5940</xsl:attribute>

is not protected by a conditional check, so it will always execute.

Use this:

<xsl:if test="$style='{StaticResource LabelText}'">
    <xsl:attribute name="Background">#FF3B596E</xsl:attribute>
</xsl:if>
<xsl:if test="not($style='{StaticResource LabelText}')">
    <xsl:attribute name="Background">#FF3B5940</xsl:attribute>
</xsl:if>

Or xsl:choose

<xsl:choose>
    <xsl:when test="$style='{StaticResource LabelText}'">
        <xsl:attribute name="Background">#FF3B596E</xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
        <xsl:attribute name="Background">#FF3B5940</xsl:attribute>
    </xsl:otherwise>
</xsl:choose>
like image 70
Wayne Avatar answered Jan 24 '23 21:01

Wayne


If you use <xsl:attribute/> multiple times in the one element context, only last one will be applied to result element.

You can separate <xsl:attribute/> instructions using <xsl:choose/> or define one <xsl:attribute/> before <xsl:if/> -- it will be used by default:

<xsl:choose>
    <xsl:when test="$style='{StaticResource LabelText}'">
        <xsl:attribute name="Background">#FF3B596E</xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
        <xsl:attribute name="Background">#FF3B5940</xsl:attribute>
    </xsl:otherwise>
</xsl:choose>

or

        <xsl:attribute name="Background">#FF3B5940</xsl:attribute>  
        <xsl:if test="$style='{StaticResource LabelText}'" >
            <xsl:attribute name="Background">#FF3B596E</xsl:attribute>
        </xsl:if>
like image 33
Phillip Kovalev Avatar answered Jan 24 '23 19:01

Phillip Kovalev