【发布时间】:2021-01-16 05:05:58
【问题描述】:
我有一组节点显示工人的薪酬以及来自两个不同系统(不同节点)的生效日期。我需要按升序/降序对这些值进行排序,无论它们来自以前的系统还是当前系统。
我尝试在 for-each 中使用排序功能,但我收到一个错误,即排序时不能传递超过 1 个节点。
<?xml version="1.0" encoding="UTF-8"?>
<Workers xmlns:ch="test/Compensation_History">
<ch:Worker_Entry>
<ch:Employee_ID>12345</ch:Employee_ID>
<ch:Compensation_History_Previous_System>
<ch:Amount>100000</ch:Amount>
<ch:Effective_Date>2018-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Previous_System>
<ch:Compensation_History_Previous_System>
<ch:Amount>95000</ch:Amount>
<ch:Effective_Date>2017-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Previous_System>
<ch:Compensation_History_Previous_System>
<ch:Amount>90000</ch:Amount>
<ch:Effective_Date>2016-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Previous_System>
<ch:Compensation_History_Current_System>
<ch:Amount>105000</ch:Amount>
<ch:Effective_Date>2019-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Current_System>
<ch:Compensation_History_Current_System>
<ch:Amount>110000</ch:Amount>
<ch:Effective_Date>2020-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Current_System>
</ch:Worker_Entry>
</Workers>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<Workers xmlns:ch="test/Compensation_History">
<ch:Worker_Entry>
<ch:Employee_ID>12345</ch:Employee_ID>
<ch:Compensation_History_Current_System>
<ch:Amount>110000</ch:Amount>
<ch:Effective_Date>2020-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Current_System>
<ch:Compensation_History_Current_System>
<ch:Amount>105000</ch:Amount>
<ch:Effective_Date>2019-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Current_System>
<ch:Compensation_History_Previous_System>
<ch:Amount>100000</ch:Amount>
<ch:Effective_Date>2018-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Previous_System>
<ch:Compensation_History_Previous_System>
<ch:Amount>95000</ch:Amount>
<ch:Effective_Date>2017-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Previous_System>
<ch:Compensation_History_Previous_System>
<ch:Amount>90000</ch:Amount>
<ch:Effective_Date>2016-01-01-07:00</ch:Effective_Date>
</ch:Compensation_History_Previous_System>
</ch:Worker_Entry>
</Workers>
下面是我试过的代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ch="test/Compensation_History" exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<Workers>
<xsl:for-each-group select="ch:Workers" group-by="ch:Employee_ID">
<xsl:sort select="//ch:Effective_Date" order="descending" />
<xsl:copy-of select="." />
</xsl:for-each-group>
</Workers>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
为什么不发布您的尝试以便我们修复它,而不必从头开始为您编写代码。
-
2016-01-01-07:00代表什么,带有时区偏移的日期或日期加时间? -
@michael.hor257k,我已经更新了我的问题。
-
@MartinHonnen,是的,那是带有时区的日期。
-
顺便说一句-这可以在XSLT 1.0中处理
标签: xml xslt xslt-2.0 xslt-3.0