title: XSL unique value key
body: <p><strong>Goal</strong></p>
  
  <p>(XSLT 1.0). My goal is to take a set of elements, S, and produce another set, T, where T contains the unique elements in S. And to do so as efficiently as possible. (Note: I don't have to create a variable containing the set, or anything like that. I just need to loop over the elements that are unique).</p>
  
  <p><strong>Example Input and Key</strong></p>
  
  <pre><code>&lt;!-- My actual input consists of a bunch of &lt;Result&gt; elements --&gt;
  &lt;AllMyResults&gt;
  &lt;Result&gt;
  &lt;someElement&gt;value&lt;/state&gt;
  &lt;otherElement&gt;value 2&lt;/state&gt;
  &lt;subject&gt;Get unique subjects!&lt;/state&gt;
  &lt;/Result&gt;
  &lt;/AllMyResults&gt;
  
  &lt;xsl:key name="SubjectKey" match="AllMyResults/Result" use="subject"/&gt;
  </code></pre>
  
  <p>I think the above works, but when I go to use my key, it is incredibly slow. Below is the code for how I use my key.</p>
  
  <pre><code>&lt;xsl:for-each select="Result[count(. | key('SubjectKey', subject)[1]) = 1]"&gt;
      &lt;xsl:sort select="subject" /&gt;
              &lt;!-- Do something with the unique subject value --&gt;
      &lt;xsl:value-of select="subject" /&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p><strong>Additional Info</strong></p>
  
  <p>I believe I am doing this wrong because it slowed down my XSL considerably. As some additional info, the  code shown above is in a separate XSL file from my main XSL file. From the main XSL, I am calling a template that contains the xsl:key and the for-each shown above. The input to this template is an xsl:param containing my node-set (similar to the example input shown above).</p>