基于Shift_Code及日期过滤job_information节点的XSLT技术求助
雇佣数据过滤与排序的XSLT实现需求
现有包含多个<job_information>子节点的雇佣数据,需按以下规则过滤并保留目标记录,同时基于<Shift_Code>、<start_date>、<end_date>排序:
- 当
<Shift_Code>有值且<Shift_Code_previous>为空时,仅保留<Shift_Code>值唯一的记录,选取该Shift_Code对应**最早变更(日期最早)**的节点 - 当
<Shift_Code>与<Shift_Code_previous>均有值时,仅保留二者组合唯一的记录,选取该组合对应最早变更的节点
本人现有XSLT代码未得到正确结果,以下是输入Payload、预期输出Payload,请求协助实现正确的XSLT逻辑。
输入Payload示例
<root> <job_information> <Shift_Code>A</Shift_Code> <Shift_Code_previous></Shift_Code_previous> <start_date>2023-05-01</start_date> <end_date>9999-12-31</end_date> <!-- 其他字段 --> </job_information> <job_information> <Shift_Code>A</Shift_Code> <Shift_Code_previous></Shift_Code_previous> <start_date>2022-01-01</start_date> <end_date>2023-04-30</end_date> <!-- 其他字段 --> </job_information> <job_information> <Shift_Code>B</Shift_Code> <Shift_Code_previous>A</Shift_Code_previous> <start_date>2023-06-01</start_date> <end_date>9999-12-31</end_date> <!-- 其他字段 --> </job_information> <job_information> <Shift_Code>B</Shift_Code> <Shift_Code_previous>A</Shift_Code_previous> <start_date>2023-05-01</start_date> <end_date>2023-05-31</end_date> <!-- 其他字段 --> </job_information> </root>
预期输出Payload示例
<root> <job_information> <Shift_Code>A</Shift_Code> <Shift_Code_previous></Shift_Code_previous> <start_date>2022-01-01</start_date> <end_date>2023-04-30</end_date> <!-- 其他字段 --> </job_information> <job_information> <Shift_Code>B</Shift_Code> <Shift_Code_previous>A</Shift_Code_previous> <start_date>2023-05-01</start_date> <end_date>2023-05-31</end_date> <!-- 其他字段 --> </job_information> </root>
解决方案XSLT代码
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <!-- 按分组规则聚合记录 --> <xsl:for-each-group select="root/job_information" group-by=" if (Shift_Code != '' and Shift_Code_previous = '') then Shift_Code else concat(Shift_Code, '|', Shift_Code_previous) "> <!-- 分组内按日期升序排序,取最早的记录 --> <xsl:sort select="Shift_Code" order="ascending"/> <xsl:sort select="start_date" order="ascending"/> <xsl:sort select="end_date" order="ascending"/> <xsl:copy-of select="current-group()[1]"/> </xsl:for-each-group> </xsl:stylesheet>
逻辑说明
- 分组规则:
- 针对
<Shift_Code>非空、<Shift_Code_previous>为空的记录,直接用<Shift_Code>作为分组键 - 针对两个字段都非空的记录,用
Shift_Code|Shift_Code_previous组合作为分组键,确保组合唯一性
- 针对
- 筛选最早记录:每个分组内的记录按
start_date升序排序,取排序后的第一条即为最早变更的记录 - 最终排序:按
<Shift_Code>、<start_date>、<end_date>的顺序升序排列结果集
内容的提问来源于stack exchange,提问作者piuser
相关产品推荐
相关产品推荐

