将参数传递给xsl模板

时间:2013-10-27 11:27:37

标签: html xml xslt

我正在调用一个xsl模板,我在其中构建一个表。我想根据参数中的值有条件地设置表的颜色。有什么建议吗?

实施例

<xsl:template match="sometemplate">
    <xsl:param name="bgcolor" select="'black'"/>
    <table>
        <!-- I have to set the background color of this tr depending on the value of bgcolor -->
        <tr>
            <td>
                <!-- Do Something -->
            </td>
        </tr>
    <table>
</xsl:template>

<xsl:template match="callingTemplate">
    <xsl:apply-templates select="sometemplate">
        <xsl:with-param name="bgcolor" select="'white'"/>
    </xsl:apply-templates>
</xsl:template>

1 个答案:

答案 0 :(得分:1)

您可以在此处使用属性值模板。将<tr>替换为:

<tr style="background-color:{$bgcolor}">

花括号表示要计算的表达式,而不是字面输出,因此它将替换为传递给模板的任何颜色名称。例如:

<tr style="background-color:white">
相关问题