title: Removing duplicates from a bag returned by xsl:key based on attribute value
body: <p>I have the following xml file.</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Bank&gt;
    &lt;Person personId="1" type="1071" deleted="0"&gt;
    &lt;/Person&gt;
    &lt;Person personId="2" type="1071" deleted="0"&gt;
    &lt;/Person&gt;
    &lt;Person personId="3" type="1071" deleted="0"&gt;
    &lt;/Person&gt;
    &lt;Account&gt;
      &lt;Role personId="1" type="1025" /&gt;
    &lt;/Account&gt;
    &lt;Account&gt;
      &lt;Role personId="1" type="1025" /&gt;
    &lt;/Account&gt;
    &lt;Account&gt;
      &lt;Role personId="1" type="1018" /&gt;
    &lt;/Account&gt;
    &lt;Account&gt;
      &lt;Role personId="3" type="1025" /&gt;
      &lt;Role personId="1" type="1018" /&gt;
    &lt;/Account&gt;
    &lt;Account&gt;
      &lt;Role personId="2" type="1025" /&gt;
    &lt;/Account&gt;
  &lt;/Bank&gt;
  </code></pre>
  
  <p>and the following xsl transformation.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;xsl:output method="text" encoding="ISO-8859-1" /&gt;
    &lt;xsl:strip-space elements="*" /&gt;
  
    &lt;xsl:key name="roleKey"
      match="Role[(@type = '1025' or @type = '1018' or @type = '1022' or @type = '1023') and not(@validTo)]"
      use="@personId" /&gt;
  
    &lt;xsl:template match="Person"&gt;
      &lt;xsl:value-of select="@personId" /&gt;
      &lt;xsl:variable name="roles" select="key('roleKey', @personId)" /&gt;
      &lt;xsl:for-each select="$roles"&gt;
        &lt;xsl:text&gt;;&lt;/xsl:text&gt;&lt;xsl:value-of select="@type" /&gt;
      &lt;/xsl:for-each&gt;
      &lt;xsl:text&gt;&amp;#xA;&lt;/xsl:text&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The actual result is following and I would like to remove the duplicated <code>type</code> values.</p>
  
  <pre class="lang-xml prettyprint-override"><code>1;1025;1025;1018;1018
  2;1025
  3;1025
  </code></pre>
  
  <p>The expected results should be like follows...</p>
  
  <pre class="lang-xml prettyprint-override"><code>1;1025;1018
  2;1025
  3;1025
  </code></pre>
  
  <p>I have tried the tips involving keyword <code>following</code> from <a href="http://www.dpawson.co.uk/xsl/sect2/N2696.html" rel="nofollow">this website</a> and also the trick with the Muenchian method. They all do not work as they seem to be browsing through the whole document and matching the duplicates for each and every <code>Person</code> element, whereas I want to remove duplicates only in a <code>Person</code> context defined by <code>personId</code> attribute.</p>
  
  <p>How do I remove those duplicates from the bag returned by <code>key</code> function? Or maybe there is a method that I can use in <code>xsl:for-each</code> to print only what I want to?</p>
  
  <p>I can only use what there is available in XSLT 1.0. I do not have a possibility to use any XSLT 2.0 processor.</p>