title: XSLT Count Distinct Values
body: <p>I have a project where I need to get and count distinct values in tags in an XML document. If you look at the XML, I am specifically trying to count the distinct number of WorkOrderNumbers for Tuesday. Later I will do Monday and so on. I have tried the following code. </p>
  
  <pre><code>    &lt;xsl:variable name="tmp" select="count(//Tuesday/TimeDetail/WorkOrderNumber[generate-id() = generate-id(key('distinctWorkOrder', .)[1])])"&gt;&lt;/xsl:variable&gt;
  </code></pre>
  
  <p>This returns 2. I have also tried this</p>
  
  <pre><code>    &lt;xsl:variable name="tmp2" select="count(//Tuesday/TimeDetail/WorkOrderNumber[not(.=/preceding-sibling::WorkOrderNumber)])"&gt;&lt;/xsl:variable&gt;
  </code></pre>
  
  <p>It returns 4. The correct answer is 3.</p>
  
  <p>I have added the XML below. Can someone point out what I am doing wrong? Thanks a lot. </p>
  
  <pre><code>    &lt;Timesheet&gt;
  &lt;WeekEnding&gt;Sep 18, 2011&lt;/WeekEnding&gt;
  &lt;JobNumber&gt;00010-5523&lt;/JobNumber&gt;
  &lt;Customer&gt;Test Company INC&lt;/Customer&gt;
  &lt;TeamMember&gt;
      &lt;EmployeeNumber&gt;10566&lt;/EmployeeNumber&gt;
      &lt;EmployeeName&gt;Employee, One&lt;/EmployeeName&gt;
      &lt;EmployeeClass&gt;Class&lt;/EmployeeClass&gt;
      &lt;Monday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;14963&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;8&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;3&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;4 - Holiday&lt;/OtherHours&gt;
              &lt;Notes&gt;I took the afternoon off.&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Monday&gt;
      &lt;Tuesday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;14963&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;5&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;15101&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;2&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Tuesday&gt;
      &lt;Wednesday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;14963&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;5&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Wednesday&gt;
      &lt;Thursday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;14963&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;5&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Thursday&gt;
      &lt;Friday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;14963&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;5&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;2&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Friday&gt;
      &lt;Saturday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;14963&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;5&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Saturday&gt;
      &lt;Sunday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;14963&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;5&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;1&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Sunday&gt;
  &lt;/TeamMember&gt;
  &lt;TeamMember&gt;
      &lt;EmployeeNumber&gt;81135&lt;/EmployeeNumber&gt;
      &lt;EmployeeName&gt;Employee, Two&lt;/EmployeeName&gt;
      &lt;EmployeeClass&gt;Class&lt;/EmployeeClass&gt;
      &lt;Monday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;15100&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;5&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;1 - Company Meetings&lt;/OtherHours&gt;
              &lt;Notes&gt;These are my notes.&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;22965&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;7&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;23570&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;Regular&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;1&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Monday&gt;
      &lt;Tuesday&gt;&lt;/Tuesday&gt;
      &lt;Wednesday&gt;&lt;/Wednesday&gt;
      &lt;Thursday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;23591&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;7&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;15100&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;1&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;1&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Test&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Thursday&gt;
      &lt;Friday&gt;&lt;/Friday&gt;
      &lt;Saturday&gt;&lt;/Saturday&gt;
      &lt;Sunday&gt;&lt;/Sunday&gt;
  &lt;/TeamMember&gt;
  &lt;TeamMember&gt;
      &lt;EmployeeNumber&gt;105779&lt;/EmployeeNumber&gt;
      &lt;EmployeeName&gt;Employee, Three&lt;/EmployeeName&gt;
      &lt;EmployeeClass&gt;W&lt;/EmployeeClass&gt;
      &lt;Monday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;19583&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;8&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;Over Time&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Monday&gt;
      &lt;Tuesday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;15099&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;1&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;7&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Tuesday&gt;
      &lt;Wednesday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;15099&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;1&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;7&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Wednesday&gt;
      &lt;Thursday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;15099&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;1&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;7&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Thursday&gt;
      &lt;Friday&gt;
          &lt;TimeDetail&gt;
              &lt;StartTime&gt;Start&lt;/StartTime&gt;
              &lt;StopTime&gt;Stop&lt;/StopTime&gt;
              &lt;WorkOrderNumber&gt;15099&lt;/WorkOrderNumber&gt;
              &lt;RegularHours&gt;1&lt;/RegularHours&gt;
              &lt;OverTimeHours&gt;7&lt;/OverTimeHours&gt;
              &lt;OtherHours&gt;Other&lt;/OtherHours&gt;
              &lt;Notes&gt;Notes&lt;/Notes&gt;
          &lt;/TimeDetail&gt;
      &lt;/Friday&gt;
      &lt;Saturday&gt;&lt;/Saturday&gt;
      &lt;Sunday&gt;&lt;/Sunday&gt;
  &lt;/TeamMember&gt;
  </code></pre>
  
  <p></p>