Grouping is a common problem in XSLT stylesheets: how do you take a list of elements and arrange them into groups. There are two main situations in which you need to do this:
The most efficient method for grouping large numbers of elements according to their content, be it the value of an attribute, of a subelement, or its textual content, is the Muenchian Method (after Steve Muench, who discovered/developed it).
Here are some posts that give more examples of grouping by content:
Here are a couple of posts that illustrate grouping by position: