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

如何在XSLT中统计去重后的员工居家办公有效天数

XSLT统计员工居家办公天数实现方案

实现代码(XSLT 2.0+ 版本,兼容绝大多数现代XSLT处理器)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wd="urn:com.workday/bsvc"> <!-- 此处wd命名空间请和你实际XML的命名空间保持一致 -->
    <xsl:output method="text" encoding="UTF-8" indent="no"/>

    <xsl:template match="/">
        <!-- 提取所有不重复的居家办公日期 -->
        <xsl:variable name="uniqueWorkDates" select="distinct-values(//wd:Time_Blocks_for_Worker_group[wd:Time_Entry_Code/wd:ID = 'TIME_ENTRY_CODE-3-97']/wd:Date)"/>
        <!-- 输出日期行 -->
        <xsl:text>Date- </xsl:text>
        <xsl:value-of select="$uniqueWorkDates" separator=","/>
        <xsl:text>&#10;</xsl:text> <!-- 换行符 -->
        <!-- 输出总天数字 -->
        <xsl:text>Total Number of days </xsl:text>
        <xsl:value-of select="count($uniqueWorkDates)"/>
    </xsl:template>
</xsl:stylesheet>

低版本兼容方案(XSLT 1.0 适用)

如果你的运行环境仅支持XSLT 1.0,使用Muenchian分组法实现去重:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wd="urn:com.workday/bsvc"> <!-- 此处wd命名空间请和你实际XML的命名空间保持一致 -->
    <xsl:output method="text" encoding="UTF-8" indent="no"/>

    <!-- 定义按日期分组的键 -->
    <xsl:key name="dateGroup" match="wd:Time_Blocks_for_Worker_group[wd:Time_Entry_Code/wd:ID = 'TIME_ENTRY_CODE-3-97']" use="wd:Date"/>

    <xsl:template match="/">
        <!-- 提取所有不重复的日期节点 -->
        <xsl:variable name="uniqueDateNodes" select="//wd:Time_Blocks_for_Worker_group[generate-id() = generate-id(key('dateGroup', wd:Date)[1])]/wd:Date"/>
        <!-- 输出日期行 -->
        <xsl:text>Date- </xsl:text>
        <xsl:for-each select="$uniqueDateNodes">
            <xsl:value-of select="."/>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#10;</xsl:text>
        <!-- 输出总天数字 -->
        <xsl:text>Total Number of days </xsl:text>
        <xsl:value-of select="count($uniqueDateNodes)"/>
    </xsl:template>
</xsl:stylesheet>

注意事项

  • 请将代码中wd命名空间的值替换为你实际XML文件根节点中声明的wd对应命名空间,否则会无法匹配节点
  • 代码中已默认过滤时间条目编码为TIME_ENTRY_CODE-3-97(即居家办公类型)的记录,如果你需要调整过滤规则可修改条件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 05:15:03