string 测试字符串中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1144889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test for a value inside a string
提问by lavinio
Let's say I have the string of "2004,2005,2006,2007,2008,2009" that is assigned to the parameter of "show".
假设我有分配给“show”参数的字符串“2004,2005,2006,2007,2008,2009”。
Now, this does work:
现在,这确实有效:
<xsl:if test="$show='2004'">
//stuff
</xsl:if>
This doesn't work:
这不起作用:
<xsl:if test="$show='2005'">
//stuff
</xsl:if>
I'm not sure how to test for part of a string like that. Any idea?
我不确定如何测试这样的字符串的一部分。任何的想法?
回答by lavinio
Use contains
.
使用contains
.
<xsl:if test="contains($show, '2004')">
//stuff
</xsl:if>
A more detailed example, which will print yes.
一个更详细的例子,它将打印yes。
<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>
回答by pixel
You're needing the contains() XPath function. You can use it like this:
您需要 contains() XPath 函数。你可以这样使用它:
<xsl:if test="contains($show,'2005')">
//stuff
</xsl:if>