Grouping in XSLT 2.0
Grouping in XSLT 1.0 can be quite difficult. Grouping with the Muenchian method is one way to do it with XSLT 1.0. You can find out more about grouping with Muenchian method in XSLT 1.0 here.
Grouping in XSLT 2.0 is easier.
Here's some XML for William Shakespare's The Tragedy of Hamlet, Prince of Denmark, Act 3, Scene 1.
We'll group speeches here by character and then group by scene.
And here's how to it with XSLT 2.0.
And here's the output.