title: How do I prevent duplicates, in XSL?
body: <p>How do I prevent duplicate entries into a list, and then ideally, sort that list?  What I'm doing, is when information at one level is missing, taking the information from a level below it, to building the missing list, in the level above.  Currently, I have XML similar to this:</p>
  
  <pre><code>&lt;c03 id="ref6488" level="file"&gt;
      &lt;did&gt;
          &lt;unittitle&gt;Clinic Building&lt;/unittitle&gt;
          &lt;unitdate era="ce" calendar="gregorian"&gt;1947&lt;/unitdate&gt;
      &lt;/did&gt;
      &lt;c04 id="ref34582" level="file"&gt;
          &lt;did&gt;
              &lt;container label="Box" type="Box"&gt;156&lt;/container&gt;
              &lt;container label="Folder" type="Folder"&gt;3&lt;/container&gt;
          &lt;/did&gt;
      &lt;/c04&gt;
      &lt;c04 id="ref6540" level="file"&gt;
          &lt;did&gt;
              &lt;container label="Box" type="Box"&gt;156&lt;/container&gt;
              &lt;unittitle&gt;Contact prints&lt;/unittitle&gt;
          &lt;/did&gt;
      &lt;/c04&gt;
      &lt;c04 id="ref6606" level="file"&gt;
          &lt;did&gt;
              &lt;container label="Box" type="Box"&gt;154&lt;/container&gt;
              &lt;unittitle&gt;Negatives&lt;/unittitle&gt;
          &lt;/did&gt;
      &lt;/c04&gt;
  &lt;/c03&gt;
  </code></pre>
  
  <p>I then apply the following XSL:</p>
  
  <pre><code>&lt;xsl:template match="c03/did"&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="not(container)"&gt;
              &lt;did&gt;
                  &lt;!-- If no c03 container item is found, look in the c04 level for one --&gt;
                  &lt;xsl:if test="../c04/did/container"&gt;
  
                      &lt;!-- If a c04 container item is found, use the info to build a c03 version --&gt;
                      &lt;!-- Skip c03 container item, if still no c04 items found --&gt;
                      &lt;container label="Box" type="Box"&gt;
  
                          &lt;!-- Build container list --&gt;
                          &lt;!-- Test for more than one item, and if so, list them, --&gt;
                          &lt;!-- separated by commas and a space --&gt;
                          &lt;xsl:for-each select="../c04/did"&gt;
                              &lt;xsl:if test="position() &amp;gt; 1"&gt;, &lt;/xsl:if&gt;
                              &lt;xsl:value-of select="container"/&gt;
                          &lt;/xsl:for-each&gt;
                      &lt;/container&gt;                    
              &lt;/did&gt;
          &lt;/xsl:when&gt;
  
          &lt;!-- If there is a c03 container item(s), list it normally --&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:copy-of select="."/&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>But I'm getting the "container" result of</p>
  
  <pre><code>&lt;container label="Box" type="Box"&gt;156, 156, 154&lt;/container&gt;
  </code></pre>
  
  <p>when what I want is</p>
  
  <pre><code>&lt;container label="Box" type="Box"&gt;154, 156&lt;/container&gt;
  </code></pre>
  
  <p>Below is the full result that I'm trying to get:</p>
  
  <pre><code>&lt;c03 id="ref6488" level="file"&gt;
      &lt;did&gt;
          &lt;container label="Box" type="Box"&gt;154, 156&lt;/container&gt;
          &lt;unittitle&gt;Clinic Building&lt;/unittitle&gt;
          &lt;unitdate era="ce" calendar="gregorian"&gt;1947&lt;/unitdate&gt;
      &lt;/did&gt;
      &lt;c04 id="ref34582" level="file"&gt;
          &lt;did&gt;
              &lt;container label="Box" type="Box"&gt;156&lt;/container&gt;
              &lt;container label="Folder" type="Folder"&gt;3&lt;/container&gt;
          &lt;/did&gt;
      &lt;/c04&gt;
      &lt;c04 id="ref6540" level="file"&gt;
          &lt;did&gt;
              &lt;container label="Box" type="Box"&gt;156&lt;/container&gt;
              &lt;unittitle&gt;Contact prints&lt;/unittitle&gt;
          &lt;/did&gt;
      &lt;/c04&gt;
      &lt;c04 id="ref6606" level="file"&gt;
          &lt;did&gt;
              &lt;container label="Box" type="Box"&gt;154&lt;/container&gt;
              &lt;unittitle&gt;Negatives&lt;/unittitle&gt;
          &lt;/did&gt;
      &lt;/c04&gt;
  &lt;/c03&gt;
  </code></pre>
  
  <p>Thanks in advance for any help!</p>