title: xslt nested for each
body: <p>I have the following xml</p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;root&gt;
      &lt;employee&gt;
          &lt;name&gt;a&lt;/name&gt;
          &lt;company&gt;1&lt;/company&gt;
      &lt;/employee&gt;
      &lt;employee&gt;
          &lt;name&gt;b&lt;/name&gt;
          &lt;company&gt;2&lt;/company&gt;
      &lt;/employee&gt;
      &lt;employee&gt;
          &lt;name&gt;c&lt;/name&gt;
          &lt;company&gt;1&lt;/company&gt;
      &lt;/employee&gt;
      &lt;employee&gt;
          &lt;name&gt;d&lt;/name&gt;
          &lt;campany&gt;2&lt;/campany&gt;
      &lt;/employee&gt;
      &lt;employee&gt;
          &lt;name&gt;e&lt;/name&gt;
          &lt;company&gt;2&lt;/company&gt;
      &lt;/employee&gt;
      &lt;employee&gt;
          &lt;name&gt;f&lt;/name&gt;
          &lt;company&gt;1&lt;/company&gt;
      &lt;/employee&gt;    
  &lt;/root&gt;
  </code></pre>
  <p>I would like to have sth like</p>
  <pre><code>&lt;root&gt;
   &lt;company&gt;
    &lt;id&gt;1&lt;/id&gt;
    &lt;employee&gt;a&lt;/employee&gt;
    &lt;employee&gt;c&lt;/employee&gt;
    &lt;employee&gt;f&lt;/employee&gt;
   &lt;/company&gt;
   &lt;company&gt;
    &lt;id&gt;2&lt;/id&gt;
    &lt;employee&gt;b&lt;/employee&gt;
    &lt;employee&gt;d&lt;/employee&gt;
    &lt;employee&gt;e&lt;/employee&gt;
   &lt;/company&gt;
  &lt;root&gt;
  </code></pre>
  <p>I tried using loops</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
      xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
      xmlns:mdti=&quot;urn:com.workday/multiDocumentTransform/Input&quot; xmlns:bc=&quot;urn:com.workday/bc&quot;
      exclude-result-prefixes=&quot;xs&quot; version=&quot;2.0&quot;&gt;
  
      &lt;xsl:output indent=&quot;yes&quot;/&gt;
      &lt;xsl:template match=&quot;/&quot;&gt;
  
          &lt;root&gt;
  
              &lt;xsl:variable name=&quot;companies&quot; select=&quot;distinct-values(root/employee/company)&quot;/&gt;
  
              &lt;xsl:for-each select=&quot;$companies&quot;&gt;
  
                  &lt;company&gt;
                      &lt;companyID&gt; &lt;xsl:value-of select=&quot;.&quot;/&gt;&lt;/companyID&gt;
                      &lt;employees&gt;
                          &lt;xsl:for-each select=&quot;root/employee[company=.]&quot;&gt;
                              &lt;employee&gt;&lt;xsl:value-of select=&quot;.&quot;/&gt;&lt;/employee&gt;
                          &lt;/xsl:for-each&gt;
                      &lt;/employees&gt;
                  &lt;/company&gt;
              &lt;/xsl:for-each&gt;
  
  
          &lt;/root&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>But it gives me the following error in the second for each:</p>
  <pre><code>The required item type of the context item for the child axis is node(), but the supplied expression {.} has item type xs:anyAtomicType
  </code></pre>
  <p>Is it possible to use nested loops here? I know I can get what I want with grouping.</p>
  <p>Thanks</p>
  <p>Przemek</p>