title: Transpose XML using Muenchian method - multiple groupings
body: <p>I need to transpose an XML file such as this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;products&gt;
      &lt;product id="10" name="Widget 1" price="15.99" category_code="T" category="Toys" manufacturer_code="SC1" manufacturer="Some Company 1" /&gt;
      &lt;product id="10" name="Widget 1" price="15.99" category_code="E" category="Electronics" manufacturer_code="SC1" manufacturer="Some Company 1" /&gt;
      &lt;product id="10" name="Widget 1" price="15.99" category_code="T" category="Toys" manufacturer_code="SC2" manufacturer="Some Company 2" /&gt;
      &lt;product id="10" name="Widget 1" price="15.99" category_code="E" category="Electronics" manufacturer_code="SC2" manufacturer="Some Company 2" /&gt;
      &lt;product id="10" name="Widget 1" price="15.99" category_code="T" category="Toys" manufacturer_code="SC3" manufacturer="Some Company 3" /&gt;
      &lt;product id="10" name="Widget 1" price="15.99" category_code="E" category="Electronics" manufacturer_code="SC3" manufacturer="Some Company 3" /&gt;
      &lt;product id="11" name="Widget 2" price="21.99" category_code="V" category="Video Games" manufacturer_code="SC4" manufacturer="Some Company 4" /&gt;
      &lt;product id="12" name="Widget 3" price="10.99" category_code="T" category="Toys" manufacturer_code="SC1" manufacturer="Some Company 2" /&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>to a comma delimited text file, or a properly formated HTML table that includes only one line for each product, something like this:</p>
  
  <pre><code>id,name,price,category_code_1,category_1,category_code_2,category_2,manufacturer_code_1,manufacturer_1,manufacturer_code_2,manufacturer_2,manufacturer_code_3,manufacturer_3
      10, Widget 1, 15.99, T, Toys, E, Electronics, SC1, Some Company 1, SC2, Some Company 2, SC3, Some Company 3 
      11, Widget 2, 21.99, V, Video Games,,, SC4, Some Company 4,,,,
      12, Widget 3, 10.99, T, Toys,,, SC1, Some Company 2,,,,
  </code></pre>
  
  <p>As you'd notice, the XML data can be thought of as the result of three tables joined together: product, product_category, and product_manufacturer. Each product can belong to multiple categories and have multiple manufacturers. Of course the real data that I'm dealing with is more complex and in a totally different domain, but this sample depicts the problem properly.</p>
  
  <p>I have very limited knowledge of XSLT, and with help of SO and other resources on the Interent, have put together a stylesheet that partly provides me what I need:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/&gt;
      &lt;xsl:key name="key_product_group" match="product" use="@id"/&gt;
      &lt;xsl:key name="key_category_group" match="product" use="concat(
               @id,
               @category_code,
               @category)"/&gt;
      &lt;xsl:key name="key_manufacturer_group" match="product" use="concat(
               @id,
               @manufacturer_code,
               @manufacturer)"/&gt;
      &lt;xsl:variable name="var_max_category_group"  &gt;
          &lt;xsl:for-each select="//product[generate-id(.) = generate-id(key('key_product_group',@id) )]"&gt;
              &lt;xsl:sort select="count(key('key_product_group',@id )[generate-id() = generate-id(key('key_category_group',
                                     concat(@id,
                                    @category_code,
                                    @category)))])" order="descending"/&gt;
              &lt;xsl:if test="position() = 1"&gt;
                  &lt;xsl:value-of select="count(key('key_product_group',@id )[generate-id() = generate-id(key('key_category_group',
                                     concat(@id,
                                    @category_code,
                                    @category)))])"/&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:variable name="var_max_manufacturer_group"&gt;
          &lt;xsl:for-each select="//product[generate-id(.) = generate-id(key('key_product_group',@id) )]"&gt;
              &lt;xsl:sort select="count(key('key_product_group',@id )[generate-id() = generate-id(key('key_manufacturer_group',
                                     concat(@id,
                                    @category_code,
                                    @category)))])" order="descending"/&gt;
              &lt;xsl:if test="position() = 1"&gt;
                  &lt;xsl:value-of   select="count(key('key_product_group',@id )[generate-id() = generate-id(key('key_manufacturer_group',
                                     concat(@id,
                                    @manufacturer_code,
                                    @manufacturer)))])"/&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:template match="/"&gt;
          &lt;xsl:text&gt;id,&lt;/xsl:text&gt;
          &lt;xsl:text&gt;name,&lt;/xsl:text&gt;
          &lt;xsl:text&gt;price,&lt;/xsl:text&gt;
          &lt;xsl:call-template name="loop_pcat"&gt;
              &lt;xsl:with-param name="count" select="$var_max_category_group"/&gt;
          &lt;/xsl:call-template&gt;
          &lt;xsl:call-template name="loop_pmf"&gt;
              &lt;xsl:with-param name="count" select="$var_max_manufacturer_group"/&gt;
          &lt;/xsl:call-template&gt;
          &lt;br&gt;&lt;/br&gt;
          &lt;xsl:variable name="var_group"
                            select="//product[generate-id(.) = generate-id(key('key_product_group',@id)[1])]"/&gt;
          &lt;xsl:for-each select="$var_group"&gt;
              &lt;xsl:sort order="ascending" select="@id"/&gt;
              &lt;xsl:value-of select="@id"/&gt;,
              &lt;xsl:value-of select="@name"/&gt;,
              &lt;xsl:value-of select="@price"/&gt;,
              &lt;xsl:for-each select="key('key_product_group',@id )[generate-id() = generate-id(key('key_category_group',
                                     concat(@id,
                                    @category_code,
                                    @category)))]"&gt;
                  &lt;xsl:value-of select="@category_code"/&gt;,
                  &lt;xsl:value-of select="@category"/&gt;,
              &lt;/xsl:for-each&gt;
              &lt;xsl:for-each select="key('key_product_group',@id )[generate-id() = generate-id(key('key_manufacturer_group',
                                     concat(@id,
                                    @manufacturer_code,
                                    @manufacturer)))]"&gt;
                  &lt;xsl:value-of select="@manufacturer_code"/&gt;,
                  &lt;xsl:value-of select="@manufacturer"/&gt;,
              &lt;/xsl:for-each&gt;
              &lt;br&gt;&lt;/br&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template name="loop_pcat"&gt;
          &lt;xsl:param name="count" select="1"/&gt;
          &lt;xsl:param name="limit" select="$count+1"/&gt;
          &lt;xsl:if test="$count &gt; 0"&gt;
              &lt;xsl:value-of select="concat('category_code_',$limit - $count)"/&gt;
              &lt;xsl:text&gt;,&lt;/xsl:text&gt;
              &lt;xsl:value-of select="concat('category_',$limit - $count)"/&gt;
              &lt;xsl:text&gt;,&lt;/xsl:text&gt;
              &lt;xsl:call-template name="loop_pcat"&gt;
                  &lt;xsl:with-param name="count" select="$count - 1"/&gt;
                  &lt;xsl:with-param name="limit" select="$limit"/&gt;
              &lt;/xsl:call-template&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template name="loop_pmf"&gt;
          &lt;xsl:param name="count" select="1"/&gt;
          &lt;xsl:param name="limit" select="$count+1"/&gt;
          &lt;xsl:if test="$count &gt; 0"&gt;
              &lt;xsl:value-of select="concat('manufacturer_code_',$limit - $count)"/&gt;
              &lt;xsl:text&gt;,&lt;/xsl:text&gt;
              &lt;xsl:value-of select="concat('manufacturer_',$limit - $count)"/&gt;
              &lt;xsl:text&gt;,&lt;/xsl:text&gt;
              &lt;xsl:call-template name="loop_pmf"&gt;
                  &lt;xsl:with-param name="count" select="$count - 1"/&gt;
                  &lt;xsl:with-param name="limit" select="$limit"/&gt;
              &lt;/xsl:call-template&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The above stylesheet produces the following results:</p>
  
  <pre><code>id,name,price,category_code_1,category_1,category_code_2,category_2,manufacturer_code_1,manufacturer_1,manufacturer_code_2,manufacturer_2,manufacturer_code_3,manufacturer_3,
  10, Widget 1, 15.99, T, Toys, E, Electronics, SC1, Some Company 1, SC2, Some Company 2, SC3, Some Company 3, 
  11, Widget 2, 21.99, V, Video Games, SC4, Some Company 4, 
  12, Widget 3, 10.99, T, Toys, SC1, Some Company 2, 
  </code></pre>
  
  <p>The output has at least one major problem: all columns do not exist in each line, for example line 2 and 3 are missing category_code_2, and category_2, and manufacture_code and manufacturer 2 and 3. I'm sure there are other problems with the stylesheet as well, and I have no idea how it would perform on a relatively large xml file, but for now I would greatly appreciate your help with making the stylesheet produce the desired output format.</p>
  
  <p>Thank you</p>
  
  <p>MRSA</p>