It's a viable approach, but not very efficient. As a simple improvement, you could replace the use of the "preceding" axis with preceding-sibling.
The problem you are tackling is generally called "grouping", and there is a whole literature on the subject. Start with http://www.jenitennison.com/xslt/grouping
. Grouping using keys (Muenchian grouping) is hard to understand at first but much more efficient for large files. XSLT 2.0 has an <xsl:for-each-group> construct which makes this kind of problem very easy to code.
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference