XSLT按连续日期分组求和:如何处理请假取消产生的负数值
解决XSLT连续日期分组中请假取消(负时长)的问题
嘿,这个问题我之前帮同事排查过,太懂你的痛点了!常规的连续日期分组只盯着日期是否连续,但忽略了一个关键情况:同一日期内正负时长抵消后(比如请假又取消),该日期其实相当于“无有效请假”,如果不先过滤掉这些无效日期,就会打乱原本的连续分组逻辑。
核心思路
我们需要分两步处理:
- 预处理:按日期聚合时长,过滤无效日期
先把同一天的所有units求和,如果总和为0,直接排除这个日期(因为请假+取消等于没请假)。 - 对有效日期进行连续分组
基于预处理后的干净数据,再用XSLT的分组功能把连续日期归为一组,计算每组的总时长、起止日期。
XSLT 2.0+ 解决方案(推荐,语法更简洁)
假设你的输入XML结构类似这样:
<leaveRecords> <record> <date>2024-05-01</date> <units>8</units> </record> <record> <date>2024-05-01</date> <units>-8</units> <!-- 取消当天请假 --> </record> <record> <date>2024-05-02</date> <units>4</units> </record> <record> <date>2024-05-03</date> <units>8</units> </record> <record> <date>2024-05-05</date> <units>8</units> </record> </leaveRecords>
对应的XSLT代码:
<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="xml" indent="yes"/> <!-- 第一步:聚合每日时长,过滤总和为0的日期 --> <xsl:variable name="dailyValidRecords"> <xsl:for-each-group select="/leaveRecords/record" group-by="date"> <xsl:variable name="dailyTotal" select="sum(current-group()/units/xs:decimal(.))"/> <!-- 只保留有有效时长的日期 --> <xsl:if test="$dailyTotal != 0"> <dailyRecord> <date><xsl:value-of select="current-grouping-key()"/></date> <totalUnits><xsl:value-of select="$dailyTotal"/></totalUnits> </dailyRecord> </xsl:if> </xsl:for-each-group> </xsl:variable> <!-- 第二步:对有效日期进行连续分组 --> <xsl:template match="/"> <leaveSummary> <!-- 按日期排序后,用group-adjacent判断是否连续 --> <xsl:for-each-group select="$dailyValidRecords/dailyRecord" group-adjacent=" xs:date(date) - xs:date(preceding-sibling::dailyRecord[1]/date) = xs:dayTimeDuration('P1D') or not(preceding-sibling::dailyRecord) "> <xsl:sort select="xs:date(date)" order="ascending"/> <leaveGroup> <startDate><xsl:value-of select="current-group()[1]/date"/></startDate> <endDate><xsl:value-of select="current-group()[last()]/date"/></endDate> <totalUnits><xsl:value-of select="sum(current-group()/totalUnits)"/></totalUnits> </leaveGroup> </xsl:for-each-group> </leaveSummary> </xsl:template> </xsl:stylesheet>
代码解释:
- 先用
for-each-group按日期分组,计算每日总时长,过滤掉总和为0的记录; - 再用
group-adjacent判断当前日期和前一个日期是否相差1天(或者是第一个记录),把连续日期归为一组; - 最后每组输出起止日期和总时长。
XSLT 1.0 解决方案(兼容旧环境)
如果你的环境只能用XSLT 1.0,没有group-adjacent,可以用递归分配分组ID的方式实现:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <!-- 按日期分组的键 --> <xsl:key name="dateKey" match="record" use="date"/> <!-- 按分组ID分组的键 --> <xsl:key name="groupIdKey" match="dailyRecord" use="@groupId"/> <!-- 第一步:预处理每日有效记录 --> <xsl:variable name="dailyValidRecords"> <xsl:for-each select="/leaveRecords/record[generate-id() = generate-id(key('dateKey', date)[1])]"> <xsl:variable name="dailyTotal" select="sum(key('dateKey', date)/units)"/> <xsl:if test="$dailyTotal != 0"> <dailyRecord> <date><xsl:value-of select="date"/></date> <total><xsl:value-of select="$dailyTotal"/></total> </dailyRecord> </xsl:if> </xsl:for-each> </xsl:variable> <!-- 递归为有效日期分配分组ID:连续日期用同一个ID --> <xsl:template name="assignGroupIds"> <xsl:param name="records"/> <xsl:param name="currentId" select="1"/> <xsl:if test="$records/dailyRecord"> <xsl:variable name="currentRecord" select="$records/dailyRecord[1]"/> <xsl:variable name="prevRecord" select="$currentRecord/preceding-sibling::dailyRecord[1]"/> <xsl:variable name="newId"> <xsl:choose> <!-- 第一个记录或和前一天不连续,ID递增 --> <xsl:when test="not($prevRecord) or (number(substring($currentRecord/date,9,2)) - number(substring($prevRecord/date,9,2)) != 1)"> <xsl:value-of select="$currentId + 1"/> </xsl:when> <!-- 连续日期,ID不变 --> <xsl:otherwise> <xsl:value-of select="$currentId"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <dailyRecord groupId="{$currentId}"> <xsl:copy-of select="$currentRecord/*"/> </dailyRecord> <!-- 递归处理剩余记录 --> <xsl:call-template name="assignGroupIds"> <xsl:with-param name="records" select="$records/dailyRecord[position() > 1]"/> <xsl:with-param name="currentId" select="$newId"/> </xsl:call-template> </xsl:if> </xsl:template> <!-- 主模板 --> <xsl:template match="/"> <xsl:variable name="groupedRecords"> <xsl:call-template name="assignGroupIds"> <xsl:with-param name="records" select="$dailyValidRecords"/> </xsl:call-template> </xsl:variable> <leaveSummary> <xsl:for-each select="$groupedRecords/dailyRecord[generate-id() = generate-id(key('groupIdKey', @groupId)[1])]"> <xsl:sort select="date"/> <leaveGroup> <startDate><xsl:value-of select="key('groupIdKey', @groupId)[1]/date"/></startDate> <endDate><xsl:value-of select="key('groupIdKey', @groupId)[last()]/date"/></endDate> <totalUnits><xsl:value-of select="sum(key('groupIdKey', @groupId)/total)"/></totalUnits> </leaveGroup> </xsl:for-each> </leaveSummary> </xsl:template> </xsl:stylesheet>
最终效果
对于示例输入,处理后会输出:
<leaveSummary> <leaveGroup> <startDate>2024-05-02</startDate> <endDate>2024-05-03</endDate> <totalUnits>12</totalUnits> </leaveGroup> <leaveGroup> <startDate>2024-05-05</startDate> <endDate>2024-05-05</endDate> <totalUnits>8</totalUnits> </leaveGroup> </leaveSummary>
完美跳过了5月1日这个无效日期,连续的5月2-3日归为一组,5月5日单独成组。
内容的提问来源于stack exchange,提问作者Arthi
相关产品推荐
相关产品推荐

