开发者

XSLT: If check parameter value

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:attr开发者_高级运维ibute>
            </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.


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>


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>


In XSLT 2.0:

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

</xsl:template>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜