title: XSLT count a distinct node
body: <p>I need to use a XSLT file to transform a xml to csv. Thats easy enough. </p>
  
  <p>I also need to count distinct values from a node and insert them to each line. This where I don't know what to do. </p>
  
  <p>The node I need to count you can see in the example below. It is called &lt; Id >. </p>
  
  <p>I basically want to count all of the id's that are unique, and insert the result into each orderline. </p>
  
  <p>Since my example have two &lt; id > nodes with the value 1 and one &lt; id > node with the value 4, the result would be 2.</p>
  
  <p>Thanks in advance for any help at all. </p>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Picked xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;OrderLines&gt;
          &lt;OrderLine&gt;
              &lt;OrderId&gt;1345
              &lt;/OrderId&gt;
              &lt;Missions&gt;
                  &lt;Mission&gt;
                      &lt;ContainerIds&gt;
                          &lt;Container&gt;
                              &lt;Id&gt;1
                              &lt;/Id&gt;
                          &lt;/Container&gt;
                      &lt;/ContainerIds&gt;
                  &lt;/Mission&gt;
              &lt;/Missions&gt;
          &lt;/OrderLine&gt;
          &lt;OrderLine&gt;
              &lt;OrderId&gt;13456
              &lt;/OrderId&gt;
              &lt;Missions&gt;
                  &lt;Mission&gt;
                      &lt;ContainerIds&gt;
                          &lt;Container&gt;
                              &lt;Id&gt;1
                              &lt;/Id&gt;
                          &lt;/Container&gt;
                      &lt;/ContainerIds&gt;
                  &lt;/Mission&gt;
              &lt;/Missions&gt;
          &lt;/OrderLine&gt;
          &lt;OrderLine&gt;
              &lt;OrderId&gt;134567
              &lt;/OrderId&gt;
              &lt;Missions&gt;
                  &lt;Mission&gt;
                      &lt;ContainerIds&gt;
                          &lt;Container&gt;
                              &lt;Id&gt;4
                              &lt;/Id&gt;
                          &lt;/Container&gt;
                      &lt;/ContainerIds&gt;
                  &lt;/Mission&gt;
              &lt;/Missions&gt;
          &lt;/OrderLine&gt;
      &lt;/OrderLines&gt;
  &lt;/Picked&gt;
  </code></pre>
  
  <p><strong>Desired result:</strong></p>
  
  <pre><code>    Orderid;Id
      1345;2
      13456;2
      134567;2
  </code></pre>
  
  <p>XSLT so far:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"&gt;
      &lt;xsl:output method="text" encoding="UTF-8"/&gt;
      &lt;xsl:template match='/'&gt;
  
      &lt;xsl:text&gt;Orderid;Id&lt;/xsl:text&gt;
      &lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt;
  
      &lt;xsl:for-each select="/Picked/OrderLines/OrderLine"&gt;
        &lt;xsl:value-of select="OrderId"/&gt;
        &lt;xsl:text&gt;;&lt;/xsl:text&gt;
        &lt;xsl:value-of select="Missions/Mission/ContainerIds/Container/Id"/&gt;                           
        &lt;xsl:if test="position()!=last()"&gt;
          &lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt;
        &lt;/xsl:if&gt;
      &lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>My Result so far:</p>
  
  <pre><code>    Orderid;Id
      1345;1
      13456;1
      134567;4
  </code></pre>