title: Display first two positions in nested tree
body: <p>I'm new to XSL and learning as I go. I'm currently editing a third party stylesheet for XML created locally, part of which looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?xml-stylesheet type="text/xsl"&gt;
  
  &lt;c01&gt;
      &lt;did&gt;
          &lt;unittitle&gt;Annual Reports&lt;/unittitle&gt;
          &lt;physdesc&gt;19 folders&lt;/physdesc&gt;                                   
      &lt;/did&gt;
      &lt;scopecontent&gt;
          &lt;p&gt;Annual reports...&lt;/p&gt;
      &lt;/scopecontent&gt;
      &lt;c02&gt;
          &lt;did&gt;
              &lt;container type="box"&gt;1&lt;/container&gt;
              &lt;container type="folder"&gt;1&lt;/container&gt;
              &lt;unittitle&gt;1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)&lt;/unittitle&gt;
          &lt;/did&gt;
      &lt;/c02&gt;
      &lt;c02&gt;
           &lt;did&gt;
              &lt;container type="folder"&gt;2&lt;/container&gt;
              &lt;unittitle&gt;1869, 1872 (BPA); 1873 (IAS)&lt;/unittitle&gt;
           &lt;/did&gt;
      &lt;/c02&gt;
  
  &lt;c01&gt;
       &lt;did&gt;
           &lt;unittitle&gt;Bulletins&lt;/unittitle&gt;
           &lt;physdesc&gt;2 folders&lt;/physdesc&gt;
       &lt;/did&gt;
       &lt;scopecontent&gt;
           &lt;p&gt;Bulletins...&lt;/p&gt;
       &lt;/scopecontent&gt;
       &lt;c02&gt;
           &lt;did&gt;
               &lt;container type="box"&gt;1&lt;/container&gt;
               &lt;container type="folder"&gt;21&lt;/container&gt;
               &lt;unittitle&gt;Bulletins 1945-46&lt;/unittitle&gt;
           &lt;/did&gt;
       &lt;/c02&gt;
       &lt;c02&gt;
           &lt;did&gt;
           &lt;container type="box"&gt;2&lt;/container&gt;
           &lt;container type="folder"&gt;1&lt;/container&gt;
           &lt;unittitle&gt;Bulletins 1946-51&lt;/unittitle&gt;
           &lt;/did&gt;
       &lt;/c02&gt;
  &lt;/c01&gt;
  </code></pre>
  
  <p>With some XSL that creates a table for the many <code>&lt;c01&gt;</code>s, <code>&lt;c02&gt;</code>s, etc, that looks partially like this:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
  &lt;xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/&gt;
  
  &lt;xsl:template match="c02/did"&gt;
  &lt;tr&gt;    
      &lt;xsl:choose&gt;
          &lt;xsl:when test="ancestor::c01/descendant::c02/did/container"&gt;
              &lt;xsl:if test="position()=1"&gt;
                  &lt;th&gt;&lt;xsl:value-of select="container[1]/@type"/&gt;&lt;/th&gt;
                  &lt;th&gt;&lt;xsl:value-of select="container[2]/@type"/&gt;&lt;/th&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;&lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/tr&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>A table is created prior to this template in the XSL which creates multiple columns, the first two of which are to have <code>&lt;th&gt;</code>s that pull the <code>&lt;container type&gt;</code> (usually "Box" and "Folder"). Each <code>&lt;container type&gt;</code> should only appear once per <code>&lt;c01&gt;</code>, which it should pull from the first <code>&lt;c02&gt;</code>. Sometimes there are <code>&lt;c02&gt;</code>s with only one <code>&lt;container type&gt;</code>, sometimes <code>&lt;c01&gt;</code>s have multiple <code>&lt;c02&gt;</code>s with both <code>&lt;container type="box"&gt;</code> &amp; <code>&lt;container type="folder"&gt;</code>.</p>
  
  <p>I've tried many variations of <code>position()=1</code> and using <code>&lt;xsl:choose&gt;</code>/<code>&lt;xsl:when&gt;</code>, pretty much everything I can think of. It either always displays a <code>&lt;th&gt;</code> for every instance of <code>&lt;container type="box"&gt;</code> &amp; <code>&lt;container type="folder"&gt;</code> or displays <code>&lt;th&gt;</code>s every time there are two <code>&lt;container type&gt;</code>s.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>Update with actual (undesired) output</strong><br>
  I was asked to provide some desired/actual XML output. This is my best attempt at conveying the actual HTML output (since I can't copy/paste it), disregarding everything from <code>&lt;c01&gt;</code> through <code>&lt;/scopecontent&gt;</code> (because I have that part figured out):</p>
  
  <pre><code>&lt;tr&gt;
  &lt;th&gt;Box&lt;/th&gt;
  &lt;th&gt;Folder&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;th&gt;Folder&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;2&lt;/td&gt;
  &lt;td&gt;1869, 1872 (BPA); 1873 (IAS)&lt;/td&gt;
  &lt;/tr&gt;
  
  ...
  
  &lt;tr&gt;
  &lt;th&gt;Box&lt;/th&gt;
  &lt;th&gt;Folder&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;21&lt;/td&gt;
  &lt;td&gt;Bulletins 1945-46&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;th&gt;Box&lt;/th&gt;
  &lt;th&gt;Folder&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;2&lt;/td&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;Bulletins 1946-51&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p><strong>Desired output</strong>  </p>
  
  <pre><code>&lt;tr&gt;
  &lt;th&gt;Box&lt;/th&gt;
  &lt;th&gt;Folder&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;1839-40, 1846 (SPP); 1852 (BPA); 1854 (SPP)&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;2&lt;/td&gt;
  &lt;td&gt;1869, 1872 (BPA); 1873 (IAS)&lt;/td&gt;
  &lt;/tr&gt;
  
  ...
  
  &lt;tr&gt;
  &lt;th&gt;Box&lt;/th&gt;
  &lt;th&gt;Folder&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;21&lt;/td&gt;
  &lt;td&gt;Bulletins 1945-46&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;2&lt;/td&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;Bulletins 1946-51&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>