title: XSLT 3-level grouping on attributes
body: <p>OK, I KNOW that variations on this have been asked and answered; I've been reading them all day, but I'm still stuck. So, here goes:</p>
  
  <p>I need to create a summary list in HTML from some XML.</p>
  
  <p>Given this XML:</p>
  
  <pre><code>&lt;Root&gt;&lt;!-- yes, I know I don't need a 'Root' element! Legacy code... --&gt;
    &lt;Plans&gt;
      &lt;Plan AreaID="1" UnitID="83"&gt;
        &lt;Part ID="9122" Name="foo" /&gt;
        &lt;Part ID="9126" Name="bar" /&gt;
      &lt;/Plan&gt;
      &lt;Plan AreaID="1" UnitID="86"&gt;
        &lt;Part ID="8650" Name="baz" /&gt;
      &lt;/Plan&gt;
      &lt;Plan AreaID="2" UnitID="26"&gt;
        &lt;Part ID="215" Name="quux" /&gt;
      &lt;/Plan&gt;
      &lt;Plan AreaID="1" UnitID="95"&gt;
        &lt;Part ID="7350" Name="meh" /&gt;
      &lt;/Plan&gt;
    &lt;/Plans&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>I need to emit:</p>
  
  <pre><code>&lt;ol&gt;
    &lt;li&gt;Area 1: 
      &lt;ol&gt;&lt;!-- units in Area 1 --&gt;
        &lt;li&gt;Unit 83: 
          &lt;ol&gt;
            &lt;li&gt;Part 9122 (foo)&lt;/li&gt;
            &lt;li&gt;Part 9126 (bar)&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
        &lt;li&gt;Unit 86: 
          &lt;ol&gt;
            &lt;li&gt;Part 8650 (baz)&lt;/li&gt;
          &lt;/ol&gt;
        &lt;li&gt;Unit 95: 
          &lt;ol&gt;
            &lt;li&gt;Part 7350 (meh)&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
      &lt;/ol&gt;&lt;!-- /units in Area 1--&gt;
    &lt;/li&gt;
    &lt;li&gt;Area 2: 
      &lt;ol&gt;&lt;!-- units in Area 2 --&gt;
        &lt;li&gt;Unit 26: 
          &lt;ol&gt;
            &lt;li&gt;Part 215 (quux)&lt;/li&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
      &lt;/ol&gt;&lt;!-- /units in Area 2--&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
  </code></pre>
  
  <p>I have the outer grouping working -- I get top-level list elements for Area 1 and 2. But I can't get the sequences of Units in the Areas -- I either get no output, or repeating the same value. I haven't even got down to the Part level :-(</p>
  
  <p>I've been working on a stylesheet like this:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  &lt;xsl:output method="html" indent="yes"/&gt;
  
  &lt;xsl:key name="kAreaID" match="Plan" use="@AreaID" /&gt;
  &lt;xsl:key name="kUnitID" match="Plan" use="@UnitID" /&gt;
  
  &lt;xsl:template match="/Root/Plans"&gt;
  &lt;html&gt;&lt;head&gt;&lt;title&gt;test grouping&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;ol&gt;
      &lt;xsl:for-each select="./Plan[generate-id(.) = 
                        generate-id( key( 'kAreaID', @AreaID )[1] )]"
      &gt;
        &lt;xsl:sort order="ascending" select="./@AreaID" /&gt;
        &lt;li&gt;Area &lt;xsl:value-of select="@AreaID"/&gt;: 
          &lt;ol&gt;
            &lt;xsl:for-each select="key( 'kUnitID', @UnitID )"&gt;
              &lt;li&gt;Unit &lt;xsl:value-of select="@UnitID"/&gt;: 
                &lt;ol&gt;
                  &lt;li&gt;(Parts go here...)&lt;/li&gt;
                &lt;/ol&gt;
              &lt;/li&gt;
            &lt;/xsl:for-each&gt;
          &lt;/ol&gt;
        &lt;/li&gt;
      &lt;/xsl:for-each&gt;
    &lt;/ol&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>