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

如何在XSLT 2.0中实现加2天且排除周末的日期计算

XSLT 2.0 实现当前日期加2个工作日(排除周末)方案

XSLT 2.0 内置了完整的日期处理函数,无需依赖外部扩展包即可实现需求,最终可直接输出HTML格式结果。

核心规则说明

  • 用current-date()直接获取当前日期,不需要XML传入日期参数
  • 用day-of-from-date($date)获取日期对应的星期数,返回值规则:1=周一、2=周二、3=周三、4=周四、5=周五、6=周六、7=周日
  • 日期偏移用xs:dayTimeDuration类型实现天级别的加减
  • 计数规则:仅统计工作日,周末不纳入计数,向后顺延直到凑够2个工作日

固定加2个工作日极简实现

该方案直接按星期分支计算偏移量,性能最高,可直接复用:

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="html" encoding="UTF-8" indent="yes"/>

  <xsl:variable name="today" select="current-date()"/>
  <xsl:variable name="weekdayToday" select="day-of-from-date($today)"/>
  <!-- 按当前星期计算总偏移天数 -->
  <xsl:variable name="offsetDays" select="
    if ($weekdayToday = (1,2,3,7)) then 2
    else if ($weekdayToday = (4,5)) then 4
    else 3
  "/>
  <xsl:variable name="targetDate" select="$today + xs:dayTimeDuration(concat('P', $offsetDays, 'D'))"/>

  <xsl:template match="/">
    <html>
      <head><meta charset="UTF-8"><title>工作日计算结果</title></head>
      <body>
        <p>当前日期:<xsl:value-of select="format-date($today, '[Y]年[M]月[D]日')"/></p>
        <p>向后推算2个工作日的结果:<xsl:value-of select="format-date($targetDate, '[Y]年[M]月[D]日')"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

通用任意工作日计算实现

如果后续需要调整推算的工作日数量,可使用递归函数实现通用逻辑,不需要修改分支判断:

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:output method="html" encoding="UTF-8" indent="yes"/>

  <!-- 定义加工作日的递归函数 -->
  <xsl:function name="fn:add-workdays" as="xs:date">
    <xsl:param name="startDate" as="xs:date"/>
    <xsl:param name="remainDays" as="xs:integer"/>
    <xsl:choose>
      <xsl:when test="$remainDays = 0">
        <xsl:sequence select="$startDate"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="nextDay" select="$startDate + xs:dayTimeDuration('P1D')"/>
        <xsl:variable name="nextDayWeek" select="day-of-from-date($nextDay)"/>
        <!-- 周末不扣减剩余工作日天数 -->
        <xsl:variable name="newRemain" select="if ($nextDayWeek = (6,7)) then $remainDays else $remainDays - 1"/>
        <xsl:sequence select="fn:add-workdays($nextDay, $newRemain)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:function>

  <!-- 调用函数计算加2个工作日的结果 -->
  <xsl:variable name="targetDate" select="fn:add-workdays(current-date(), 2)"/>
  <xsl:variable name="today" select="current-date()"/>

  <xsl:template match="/">
    <html>
      <head><meta charset="UTF-8"><title>工作日计算结果</title></head>
      <body>
        <p>当前日期:<xsl:value-of select="format-date($today, '[Y]年[M]月[D]日')"/></p>
        <p>向后推算2个工作日的结果:<xsl:value-of select="format-date($targetDate, '[Y]年[M]月[D]日')"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

结果验证

完全匹配需求给出的示例:

  • 若当前为2022年7月14日(周四,对应weekdayToday=4),偏移4天,结果为2022年7月18日(周一)
  • 若当前为2022年7月15日(周五,对应weekdayToday=5),偏移4天,结果为2022年7月19日(周二)

注意:如果运行环境时区和业务要求时区不一致,可对当前日期做时区调整,例如指定东八区可将current-date()替换为adjust-date-to-timezone(current-date(), xs:dayTimeDuration('PT8H'))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 11:06:19