I am new to XSLT and changing it manually will take a lot of time.
If it is XSLT 2.0 then you can use a nested
(I'm assuming your input file has a root element and uses no namespaces).
If it's XSLT 1.0 then you need to use Muenchian Grouping:
Here I'm doing a single grouping pass based on both the ID and Key attributes by creating a synthetic key value of {ID}___{Key}.