title: XSLT Ignore duplicate elements
body: <p>I have just started to learn XSLT and am having trouble ignoring duplicated elements.</p>
  
  <p>I have been searching through Stack Overflow and have seen people ask similar questions. I tried a small example to see where I was going wrong on my file, and was able to ignore duplicated elements. However the problem for me seems to arise when I have more than one type of an element.</p>
  
  <p>For Example:</p>
  
  <p>File1.xml</p>
  
  <pre><code>&lt;?xml-stylesheet type="text/xsl" href="merge2.xsl"?&gt;
  
  &lt;Main&gt;
      &lt;Records&gt;
          &lt;Record&gt;
              &lt;Description&gt;A&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;A&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;B&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;C&lt;/Description&gt;
          &lt;/Record&gt;
      &lt;/Records&gt;
  &lt;/Main&gt;
  </code></pre>
  
  <p>merge2.xsl</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/Transform"&gt;
  &lt;xsl:output method="html"/&gt;
  &lt;xsl:template match="/"&gt;
      &lt;table border="1"&gt;
          &lt;tr&gt;
              &lt;th&gt;Type&lt;/th&gt;
              &lt;th&gt;Count&lt;/th&gt;
          &lt;/tr&gt;
          &lt;xsl:for-each select="Main/Records/Record"&gt;
              &lt;xsl:if test ="not(preceding-sibling::Record(Description/text() = current()/Description/text()])"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;xsl:value-of select="Description"/&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;xsl:value-of select="count(//Record[Description/text()=current()/Description/text()])"/&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:for-each&gt;
      &lt;/table&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>This works fine and gives me the desired results.</p>
  
  <pre><code>Type    Count
   A        2
   B        1
   C        1
  </code></pre>
  
  <p>However if I were to add another Records element it seems to process the two one after another e.g.</p>
  
  <pre><code>&lt;?xml-stylesheet type="text/xsl" href="merge2.xsl"?&gt;
  
  &lt;Main&gt;
      &lt;Records&gt;
          &lt;Record&gt;
              &lt;Description&gt;A&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;A&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;B&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;C&lt;/Description&gt;
          &lt;/Record&gt;
      &lt;/Records&gt;
      &lt;Records&gt;
          &lt;Record&gt;
              &lt;Description&gt;B&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;A&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;C&lt;/Description&gt;
          &lt;/Record&gt;
          &lt;Record&gt;
              &lt;Description&gt;C&lt;/Description&gt;
          &lt;/Record&gt;
      &lt;/Records&gt;
  &lt;/Main&gt;
  </code></pre>
  
  <p>This would produce the following.</p>
  
  <pre><code>Type        Count
   A            3
   B            2
   C            3
   B            2
   A            3
   C            3
  </code></pre>
  
  <p>Where it seems to process the first instance of Records, then move onto the next. Is there any way of making it so that it would remove duplicates across the two?</p>
  
  <p>I have tried changing the for-each to go through each instance of Records, and have tried to create a separate template for it, however I still seem to be missing something as I have not managed to get it working.</p>
  
  <p>Many thanks for any help.</p>