xslt grouping by every attribute
===

I have multiple types of xml messages I need to "compact" by grouping multiple nodes under the same parent (same parent meaning they share the same node name and every attribute declared is also equal). For example:

    <TopLevel CodeTL="Something">
        <Ratings>
              <Rating CodeA="ABC" Start="1-1-2012" End="1-2-2012">
                  <RatingByNumber Code="X" Rating="10" Number="1">
                  <RatingByNumber Code="X" Rating="19" Number="2">
              </Rating>
        </Ratings>
    </TopLevel>
	    <TopLevel CodeTL="Something">
        <Ratings>
              <Rating CodeA="ABC" Start="1-2-2012" End="1-3-2012">
                  <RatingByNumber Code="X" Rating="10" Number="1">
                  <RatingByNumber Code="X" Rating="19" Number="2">
              </Rating>
        </Ratings>
    </TopLevel>
    <TopLevel CodeTL="Something">
        <Ratings>
              <Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
                  <RatingByNumber Code="X" Rating="10" Number="1">
                  <RatingByNumber Code="X" Rating="19" Number="2">
              </Rating>
        </Ratings>
    </TopLevel>
    <TopLevel CodeTL="Something">
        <Ratings>
              <Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
                  <RatingByNumber Code="X" Rating="30" Number="3">
                  <RatingByNumber Code="X" Rating="39" Number="4">
              </Rating>
        </Ratings>
    </TopLevel>

Notice how they all share the same CodeTL attribute and the last 2 share the same CodeA,Start and End attributes so what I need is to produce the following output using a xslt

    <TopLevel CodeTL="Something">
        <Ratings>
              <Rating CodeA="ABC" Start="1-1-2012" End="1-2-2012">
                  <RatingByNumber Code="X" Rating="10" Number="1">
                  <RatingByNumber Code="X" Rating="19" Number="2">
              </Rating>
              <Rating CodeA="ABC" Start="1-2-2012" End="1-3-2012">
                  <RatingByNumber Code="X" Rating="10" Number="1">
                  <RatingByNumber Code="X" Rating="19" Number="2">
              </Rating>
              <Rating CodeA="XYZ" Start="1-2-2012" End="1-3-2012">
                  <RatingByNumber Code="X" Rating="10" Number="1">
                  <RatingByNumber Code="X" Rating="19" Number="2">
                  <RatingByNumber Code="X" Rating="30" Number="3">
                  <RatingByNumber Code="X" Rating="39" Number="4">
              </Rating>
        </Ratings>
    </TopLevel>

which is much cleaner and, depending on the application consuming it, it might save processing time and saves space.

The problem I'm having is that I have different types of xml messages with different node names and attributes (and number of attributes) but they all share the same structure I'm showing here.
It would be great a generic way to handle all of them but I would be grateful for a XSLT to transform the example I provided so I can create custom code for every xml message I need to send out.