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:

Grouping By Content

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:

Grouping By Position

Here are a couple of posts that illustrate grouping by position:

/xslt/grouping/index.xml by Jeni Tennison; generated using SAXON 6.5 from Michael Kay