XPath/XSLT contains() 用于多个字符串 [英] XPath/XSLT contains() for multiple strings
问题描述
contains()
是否适用于存储在变量中的多个单词?下面一个会起作用吗?如果它不起作用,请帮我解决.
Does contains()
work for multiple words stored in a variable? Will the below one work? If it won't work, please help me with a solution.
<xsl:variable name="games" select="footballvolleyballchesscarrom"/>
<xsl:when test="contains($games,'chess,carrom')">CORRECT</xsl:when>
推荐答案
假设您要进行子字符串包含的逻辑 OR...
首先,这个select
很可能是错误的1:
First of all, this select
is likely wrong1:
<xsl:variable name="games" select="footballvolleyballchesscarrom"/>
它正在寻找一个名为 footballvolleyballchesscarrom
的元素.如果您尝试将游戏设置为 string footballvolleyballchesscarrom
,请将其更改为:
It's looking for an element named footballvolleyballchesscarrom
. If you're trying to set games to the string footballvolleyballchesscarrom
, change it to this:
<xsl:variable name="games" select="'footballvolleyballchesscarrom'"/>
然后,使用任一
<xsl:when test="contains($games,'chess')
or contains($games,'carrom')">CORRECT</xsl:when>
或
<xsl:when test="matches($games,'chess|carrom')">CORRECT</xsl:when>
测试 $games
是否包含您的多个子字符串之一.
to test whether $games
contains one of your multiple substrings.
1 如果您真的打算从此处的输入 XML 中选择(可能是多个)元素,还有另一个 XSLT 2.0 选项可用于针对一组可能的值测试所选序列.如果您的输入 XML 是,例如,
1 If you really intended to select (possibly multiple) elements from the input XML here, there is another XSLT 2.0 option for testing the selected sequence against a set of possible values. If your input XML were, say,
<games>
<game>football</game>
<game>volleyball</game>
<game>chess</game>
<game>carrom</game>
</games>
然后是这个模板,
<xsl:template match="/">
<xsl:variable name="games" select="//game"/>
<xsl:if test="$games = ('chess','carrom')">CORRECT</xsl:if>
</xsl:template>
将输出 CORRECT
因为在所选元素的字符串值中至少有 chess
或 carrom
之一.
would output CORRECT
because among the string values of the selected elements is at least one of chess
or carrom
.
这篇关于XPath/XSLT contains() 用于多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!