You say you've tried many ways, but you don't say what you tried.
For XSLT 2.0, use xsl:for-each-group. That should make this problem very easy.
For XSLT 1.0, use Muenchian grouping. You can find that covered in most textbooks, or google for Jeni Tennison's tutorial on the subject. It's seems very tricky at first, but you get the hang of it.
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference