#xslt
#xslt
Вопрос:
Следующий код в xslt (я вырезал нерелевантные части, get-textblock намного длиннее и содержит множество параметров, которые все переданы правильно):
<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>
Параметром стиля может быть либо ‘{StaticResource LabelText}’, либо ‘{StaticResource ValueText}’, и фон границы зависит от этого значения.
Однако эта структура if терпит неудачу, она всегда рисует границу FF3B5940 в моем файле output.xaml. Я вызываю шаблон следующим образом:
<xsl:call-template name="get-textblock">
<xsl:with-param name="style">{StaticResource LabelText}</xsl:with-param>
</xsl:call-template>
Кто-нибудь видит, в чем может быть проблема? Спасибо.
Ответ №1:
Строка:
<xsl:attribute name="Background">#FF3B5940</xsl:attribute>
не защищен условной проверкой, поэтому он всегда будет выполняться.
Используйте это:
<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>
Или 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>
Комментарии:
1. … О Боже, ты не можешь поверить, насколько глупо я себя чувствую, что просмотрел такую тривиальность : D Спасибо. (дайте мне 6 минут, чтобы принять этот ответ: p)
2. @Matthias — Если ты чувствуешь себя плохо, просто выведи это:
<Border Background="{concat(substring('#FF3B596E', 1 div ($style='{StaticResource LabelText}')), substring('#FF3B5940', 1 div not($style='{StaticResource LabelText}')))}"/>
Ответ №2:
Если вы используете <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>