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

XSLT position()优化问题:如何自动匹配第1、4、7…序列元素

实现XSLT中按间隔3的位置自动匹配样式类

当然可以实现自动化判断,你只需要把第一个<xsl:when>的test条件替换成position() mod 3 = 1即可。这个表达式会匹配所有位置为1、4、7、10……这类除以3余数为1的元素,完全替代硬编码的多个or判断,而且能自动适配后续更多符合规律的位置。

修改后的完整代码如下:

<xsl:choose>
    <xsl:when test="position() mod 3 = 1">
        <xsl:attribute name="class">gallery-content gallery-content-large fadeInUpBig wow</xsl:attribute>
    </xsl:when>
    <xsl:when test="position() mod 2 = 0">
        <xsl:attribute name="class">gallery-content gallery-content-small fadeInLeftBig wow</xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
        <xsl:attribute name="class">gallery-content gallery-content-small fadeInRightBig wow</xsl:attribute>
    </xsl:otherwise>
</xsl:choose>

逻辑说明

  • position() mod 3 = 1:计算元素位置除以3的余数,当余数为1时,就会触发第一个样式规则,正好对应1、4、7、10……这类间隔3的序列。
  • 原有的偶数位置判断和剩余位置的规则保持不变,不会影响其他元素的样式分配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 03:55:25