I have a set of nodes that display compensation of workers along with effective date from two different systems (different nodes). I need to sort these values in ascending / descending order irrespective of whether they are from previous system or current system.
I tried to use the sort function in the for-each, but I am getting an error that not more than 1 node can be passed while sorting.
<?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>
Expected Output:
<?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>
Below is the code that I tried:
<?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>
If you really need an XSLT 2 solution then the following should work: