XSLT: if validation parameter value

The following code is in xslt (I cut out irrelevant parts, get-textblock is much longer and has many parameters that 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 "{StaticResource LabelText}" or "{StaticResource ValueText}", and the border background depends on this value.

If the structure does not work, it always draws the border of FF3B5940 in my output.xaml file. I invoke the template as follows:

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

Does anyone see what could be the problem? Thank.

+3
source share
3 answers

Line:

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

not protected by conditional check, so it will always be executed.

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>
+6
source

<xsl:attribute/> , .

<xsl:attribute/> <xsl:choose/> <xsl:attribute/> <xsl:if/> - :

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

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

XSLT 2.0:

<xsl:template name="get-textblock">
   <xsl:param name="style"/>   
   <Border Background="{if ($style='{StaticResource LabelText}') 
                        then '#FF3B596E' 
                        else '#FF3B5940'}"/>         

</xsl:template>
+1

All Articles