You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT 2.0技术实现:检测字符串是否以数组中元素结尾

检测字符串是否以预设数组中的任意后缀结尾(XSLT实现)

你的代码问题在于ends-with()函数的第二个参数仅支持单个字符串,无法直接传入字符串序列(即你定义的后缀数组)。以下是针对不同XSLT版本的解决方案:

XSLT 2.0 及以上版本(推荐)

利用some...satisfies表达式遍历后缀序列,只要有一个后缀匹配就返回true:

<xsl:template match="card">
    <xsl:variable name="word" select="k" />
    <xsl:variable name="obviouslyRussianSuffixes" select="('изм', 'ия', 'ист', 'ёр', 'ца', 'фон')" />
    <xsl:if test="some $s in $obviouslyRussianSuffixes satisfies ends-with($word, $s)">
        <xsl:value-of select="$word" />
        <xsl:text>&#xa;</xsl:text>
    </xsl:if>
</xsl:template>

也可以用exists()函数结合过滤逻辑实现,效果完全一致:

<xsl:if test="exists($obviouslyRussianSuffixes[ends-with($word, .)])">
    <!-- 输出逻辑 -->
</xsl:if>

XSLT 1.0 版本兼容方案

XSLT 1.0不支持序列遍历和ends-with()函数,需要手动模拟后缀匹配逻辑:

<xsl:template match="card">
    <xsl:variable name="word" select="k" />
    <xsl:variable name="hasMatch">
        <xsl:for-each select="('изм', 'ия', 'ист', 'ёр', 'ца', 'фон')">
            <xsl:variable name="suffix" select="." />
            <xsl:if test="string-length($word) >= string-length($suffix) 
                          and substring($word, string-length($word) - string-length($suffix) + 1) = $suffix">
                <xsl:text>true</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <xsl:if test="$hasMatch = 'true'">
        <xsl:value-of select="$word" />
        <xsl:text>&#xa;</xsl:text>
    </xsl:if>
</xsl:template>

这里通过substring()截取字符串末尾与后缀长度相同的部分,再进行相等判断,模拟ends-with的效果;通过xsl:for-each遍历后缀,只要找到匹配项就输出true,最终通过判断变量$hasMatch的值来执行后续逻辑。

内容的提问来源于stack exchange,提问作者xinatanil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 06:30:48