title: XSLT to remove duplicate values while concating
body: <p>My XML looks like folloing:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;
  &lt;BATCHES&gt;
      &lt;item&gt;
          &lt;Material&gt;1000000079&lt;/Material&gt;
          &lt;Description&gt;330 Bulk&lt;/Description&gt;
          &lt;Tank&gt;T123&lt;/Tank&gt;
          &lt;Batch&gt;2013225287&lt;/Batch&gt;
          &lt;Quantity&gt;510&lt;/Quantity&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;Material&gt;1000000079&lt;/Material&gt;
          &lt;Description&gt;330 Bulk&lt;/Description&gt;
          &lt;Tank&gt;T123&lt;/Tank&gt;
          &lt;Batch&gt;2013225301&lt;/Batch&gt;
          &lt;Quantity&gt;520&lt;/Quantity&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;Material&gt;1000000196&lt;/Material&gt;
          &lt;Description&gt;340R Bulk&lt;/Description&gt;
          &lt;Tank&gt;T700&lt;/Tank&gt;
          &lt;Batch&gt;1000188378&lt;/Batch&gt;
          &lt;Quantity&gt;510&lt;/Quantity&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;Material&gt;1000002754&lt;/Material&gt;
          &lt;Description&gt;43 Bulk&lt;/Description&gt;
          &lt;Tank&gt;T515&lt;/Tank&gt;
          &lt;Batch&gt;2013180125&lt;/Batch&gt;
          &lt;Quantity&gt;300&lt;/Quantity&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;Material&gt;1000002754&lt;/Material&gt;
          &lt;Description&gt;43 Bulk&lt;/Description&gt;
          &lt;Tank&gt;T515&lt;/Tank&gt;
          &lt;Batch&gt;2013203124&lt;/Batch&gt;
          &lt;Quantity&gt;200&lt;/Quantity&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;Material&gt;1000002754&lt;/Material&gt;
          &lt;Description&gt;43 Bulk&lt;/Description&gt;
          &lt;Tank&gt;T515&lt;/Tank&gt;
          &lt;Batch&gt;2013214839&lt;/Batch&gt;
          &lt;Quantity&gt;700&lt;/Quantity&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;Material&gt;1000002754&lt;/Material&gt;
          &lt;Description&gt;43 Bulk&lt;/Description&gt;
          &lt;Tank&gt;T517&lt;/Tank&gt;
          &lt;Batch&gt;2013214342&lt;/Batch&gt;
          &lt;Quantity&gt;890&lt;/Quantity&gt;
      &lt;/item&gt;
  &lt;/BATCHES&gt;
  </code></pre>
  
  <p>My original XSLT look like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
      &lt;xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/&gt;
      &lt;xsl:template match="/"&gt;
          &lt;Rowsets&gt;
              &lt;Rowset&gt;
                  &lt;xsl:variable name="materials" select=".//item[Tank!='RECV' and Tank!='PARK'] "/&gt;
                  &lt;xsl:for-each select="$materials"&gt;
                      &lt;xsl:if test="generate-id(.)=                       generate-id($materials[Material=current()/Material])"&gt;
                          &lt;Row&gt;
                              &lt;Material&gt;
                                  &lt;xsl:value-of select="Material"/&gt;
                              &lt;/Material&gt;
                              &lt;Description&gt;
                                  &lt;xsl:value-of select="Description"/&gt;
                              &lt;/Description&gt;
                              &lt;Value&gt;
                                  &lt;xsl:for-each select="$materials[Material=current()/Material]/Tank"&gt;
                                      &lt;xsl:if test="node()"&gt;
                                          &lt;xsl:value-of select="concat(.,'||')"/&gt;
                                      &lt;/xsl:if&gt;
                                  &lt;/xsl:for-each&gt;
                              &lt;/Value&gt;
                          &lt;/Row&gt;
                      &lt;/xsl:if&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/Rowset&gt;
          &lt;/Rowsets&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The result of this XSLT looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;Rowsets&gt;
  &lt;Rowset&gt;
  &lt;Row&gt;
  &lt;Material&gt;1000000079&lt;/Material&gt;
  &lt;Description&gt;330 Bulk&lt;/Description&gt;
  &lt;Value&gt;T123||T123||&lt;/Value&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
  &lt;Material&gt;1000000196&lt;/Material&gt;
  &lt;Description&gt;340R Bulk&lt;/Description&gt;
  &lt;Value&gt;T700||&lt;/Value&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
  &lt;Material&gt;1000002754&lt;/Material&gt;
  &lt;Description&gt;43 Bulk&lt;/Description&gt;
  &lt;Value&gt;T515||T517||&lt;/Value&gt;
  &lt;/Row&gt;
  &lt;/Rowset&gt;
  &lt;/Rowsets&gt;
  </code></pre>
  
  <p>I wanted to remove duplicate tanks while concatinating it in Value field. So I changed my XSLT to follwoing:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
      &lt;xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/&gt;
      &lt;xsl:template match="/"&gt;
          &lt;Rowsets&gt;
              &lt;Rowset&gt;
                  &lt;xsl:variable name="materials" select=".//item[Tank!='RECV' and Tank!='PARK' and Quantity &amp;gt; 500]"/&gt;
                  &lt;xsl:for-each select="$materials"&gt;
                      &lt;xsl:if test="generate-id(.)= generate-id($materials[Material=current()/Material])"&gt;
                          &lt;Row&gt;
                              &lt;Material&gt;
                                  &lt;xsl:value-of select="Material"/&gt;
                              &lt;/Material&gt;
                              &lt;Description&gt;
                                  &lt;xsl:value-of select="Description"/&gt;
                              &lt;/Description&gt;
                              &lt;Value&gt;
                                  &lt;xsl:for-each select="$materials[Material=current()/Material]/Tank[not(.=preceding::Tank)]"&gt;
                                      &lt;xsl:if test="node()"&gt;
                                          &lt;xsl:value-of select="concat(.,'||')"/&gt;
                                      &lt;/xsl:if&gt;
                                  &lt;/xsl:for-each&gt;
                              &lt;/Value&gt;
                          &lt;/Row&gt;
                      &lt;/xsl:if&gt;
                  &lt;/xsl:for-each&gt;
              &lt;/Rowset&gt;
          &lt;/Rowsets&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>My result now looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;Rowsets&gt;
  &lt;Rowset&gt;
  &lt;Row&gt;
  &lt;Material&gt;1000000079&lt;/Material&gt;
  &lt;Description&gt;330 Bulk&lt;/Description&gt;
  &lt;Value&gt;T123||&lt;/Value&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
  &lt;Material&gt;1000000196&lt;/Material&gt;
  &lt;Description&gt;340R Bulk&lt;/Description&gt;
  &lt;Value&gt;T700||&lt;/Value&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
  &lt;Material&gt;1000002754&lt;/Material&gt;
  &lt;Description&gt;43 Bulk&lt;/Description&gt;
  &lt;Value&gt;T517||&lt;/Value&gt;
  &lt;/Row&gt;
  &lt;/Rowset&gt;
  &lt;/Rowsets&gt;
  </code></pre>
  
  <p>It removed the duplicate tank T123 for material 1000000079 but for material 1000002754, it even removed T515which should appear in Value field as its quantity is greater than 500 for folloing:</p>
  
  <pre><code>&lt;item&gt;
              &lt;Material&gt;1000002754&lt;/Material&gt;
              &lt;Description&gt;43 Bulk&lt;/Description&gt;
              &lt;Tank&gt;T515&lt;/Tank&gt;
              &lt;Batch&gt;2013214839&lt;/Batch&gt;
              &lt;Quantity&gt;700&lt;/Quantity&gt;
          &lt;/item&gt;
  </code></pre>
  
  <p>what am I doing wrong here?</p>