如何使用XSLT获取用户-课程分组记录的最晚完成日期与最早要求日期
问题原因
你当前的代码仅实现了按User+Curriculum_ID分组并保留每组第一条记录的逻辑,没有针对分组内的Completion Date和Required Date做最值计算,因此无法得到正确的最早要求日期。
修正后的XSLT代码
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <!-- 原分组键保留 --> <xsl:key name="group-key" match="Employee" use="concat(User, '|', Curriculum_ID)"/> <!-- 身份转换模板保留 --> <xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template> <!-- 分组处理逻辑重写 --> <xsl:template match="Employee[generate-id() = generate-id(key('group-key', concat(User, '|', Curriculum_ID))[1])]"> <xsl:variable name="currentGroup" select="key('group-key', concat(User, '|', Curriculum_ID))"/> <xsl:copy> <!-- 复制通用不变字段 --> <xsl:copy-of select="User"/> <xsl:copy-of select="Curriculum_ID"/> <!-- 取最晚完成日期:按Completion Date降序取第一条 --> <xsl:for-each select="$currentGroup"> <xsl:sort select="Completion_Date" order="descending" data-type="text"/> <xsl:if test="position() = 1"> <xsl:copy-of select="Completion_Date"/> </xsl:if> </xsl:for-each> <!-- 取最早要求日期:按Required Date升序取第一条 --> <xsl:for-each select="$currentGroup"> <xsl:sort select="Required_Date" order="ascending" data-type="text"/> <xsl:if test="position() = 1"> <xsl:copy-of select="Required_Date"/> </xsl:if> </xsl:for-each> <!-- 其他需要保留的字段可以在这里补充复制逻辑 --> </xsl:copy> </xsl:template> <!-- 屏蔽非分组首条的Employee节点 --> <xsl:template match="Employee"/> <!-- 原ns0:Message2处理逻辑保留 --> <xsl:template match="ns0:Message2"> <xsl:copy> <xsl:copy-of select="root[1]"/> </xsl:copy> </xsl:template> <xsl:template match="ns0:Message2/root"/> </xsl:stylesheet>
注意事项
- 上述代码默认你的日期格式为
YYYY-MM-DD类可直接按文本排序的格式,如果你的日期格式为DD/MM/YYYY等特殊格式,需要先写转换逻辑把日期转成可排序的数字格式再做比较。 - 如果有其他需要随分组保留的字段,可以在分组处理模板中对应位置补充复制逻辑。
内容的提问来源于stack exchange,提问作者Gladys
相关产品推荐
相关产品推荐

