title: XSL 1.0 Count Distinct with "following:[element]" and exclude certain elements
body: <p>So I've been struggling with counting distinct elements. I have this data. Code is ready for copy paste.</p>
  
  <pre><code>&lt;CustInvoiceTable class="entity"&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000088&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000088&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000091&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000091&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000098&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000098&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000086&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000086&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000062&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;2&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000062&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;2&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000111&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000111&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000089&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000089&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000092&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000092&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000101&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000101&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000102&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000102&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000083&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000083&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000067&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000067&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;1&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
  
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000125&lt;/ItemId&gt;
          &lt;McsCmBilProductItem class="entity"&gt;
              &lt;CgiBundleLines&gt;0&lt;/CgiBundleLines&gt;
          &lt;/McsCmBilProductItem&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
  
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000069&lt;/ItemId&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
  
      &lt;!-- Excluding these 3 elements will result in the correct count --&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000083&lt;/ItemId&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000092&lt;/ItemId&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
      &lt;McsCmBilCalcInvoiceLine class="entity"&gt;
          &lt;ItemId&gt;ITM-0000098&lt;/ItemId&gt;
      &lt;/McsCmBilCalcInvoiceLine&gt;
  
  &lt;/CustInvoiceTable&gt;
  </code></pre>
  
  <p>I want to count distinct ItemId values where CgiBundleLines = 1. The result must be 11. My XSL:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
      &lt;xsl:key name="Lines-by-ItemId" match="CustInvoiceTable/McsCmBilCalcInvoiceLine" use="CustInvoiceTable/McsCmBilCalcInvoiceLine/ItemId" /&gt;
  
      &lt;xsl:output method="xml" indent="yes" /&gt;
  
      &lt;xsl:template match="/"&gt;
  
          &lt;CountOne&gt;&lt;xsl:value-of select="count(CustInvoiceTable/McsCmBilCalcInvoiceLine[not(ItemId = following::ItemId) and McsCmBilProductItem/CgiBundleLines = 1])"/&gt;&lt;/CountOne&gt;
          &lt;CountOne&gt;&lt;xsl:value-of select="count(CustInvoiceTable/McsCmBilCalcInvoiceLine[not(ItemId = following::ItemId) and McsCmBilProductItem/CgiBundleLines = 1 and boolean(McsCmBilProductItem) = 1])"/&gt;&lt;/CountOne&gt;
  
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>When I run this i get a count of 8. It looks like the last 3 McsCmBilCalcInvoiceLine elements causes the ItemId's not to be counted. Commeting these 3 returns the count = 11, which is what i want. So how do I exclude the last 3 elements or make my XSL code not count them.</p>
  
  <p>Thank you.</p>