测试字符串内的值

时间:2009-07-17 18:17:38

标签: string testing xslt parameters

假设我将“2004,2005,2006,2007,2008,2009”字符串分配给“show”参数。

现在,这确实有效:

<xsl:if test="$show='2004'">
    //stuff
</xsl:if>

这不起作用:

<xsl:if test="$show='2005'">
    //stuff
</xsl:if>

我不确定如何测试这样的字符串的一部分。有什么想法吗?

2 个答案:

答案 0 :(得分:19)

使用contains

<xsl:if test="contains($show, '2004')">
    //stuff
</xsl:if>

更详细的示例,将打印

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="show" select="'2004,2005,2006,2007,2008,2009'"/>
        <xsl:if test="contains($show, '2004')">yes</xsl:if>
        <xsl:if test="not(contains($show, '2004'))">no</xsl:if>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:3)

您需要contains()XPath函数。您可以像这样使用它:

<xsl:if test="contains($show,'2005')">
  //stuff
</xsl:if>