title: XSLT filtering by item value
body: <p>I am trying to sum items only if the code is unique. </p>
  
  <p>I have this XML:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ITEM&gt;
      &lt;NAME&gt;Brambory 10 kg&lt;/NAME&gt;
      &lt;CODE&gt;4&lt;/CODE&gt;
      &lt;AMOUNT&gt;23&lt;/AMOUNT&gt;
      &lt;COUNT&gt;19&lt;/COUNT&gt;
  &lt;/ITEM&gt;
  &lt;ITEM&gt;
      &lt;NAME&gt;Česnek 1 kg&lt;/NAME&gt;
      &lt;CODE&gt;89&lt;/CODE&gt;
      &lt;AMOUNT&gt;11&lt;/AMOUNT&gt;
      &lt;COUNT&gt;10&lt;/COUNT&gt;
  &lt;/ITEM&gt;
  &lt;ITEM&gt;
      &lt;NAME&gt;Neděle - Příbram&lt;/NAME&gt;
      &lt;CODE&gt;SHIPPING140&lt;/CODE&gt;
      &lt;AMOUNT&gt;16&lt;/AMOUNT&gt;
      &lt;COUNT&gt;16&lt;/COUNT&gt;
  &lt;/ITEM&gt;
  &lt;ITEM&gt;
      &lt;NAME&gt;Úterý - Liberec&lt;/NAME&gt;
      &lt;CODE&gt;SHIPPING122&lt;/CODE&gt;
      &lt;AMOUNT&gt;10&lt;/AMOUNT&gt;
      &lt;COUNT&gt;10&lt;/COUNT&gt;
  &lt;/ITEM&gt;
  </code></pre>
  
  <p>I would like to sum only amounts of goods, if the delivery place is like <code>SHIPPING122</code>.</p>
  
  <p>I use this XSLT:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
   &lt;xsl:output method="xml" omit-xml-declaration="no" indent="yes"/&gt;
   &lt;xsl:strip-space elements="*"/&gt;
    &lt;xsl:key name="itemkey" match="ITEM" use="concat(NAME, CODE)"/&gt;
  &lt;xsl:template match="/ORDERS"&gt;
    &lt;xsl:copy&gt; 
        &lt;xsl:apply-templates select="descendant::ITEM[generate-id() = generate-id(key('itemkey', concat(NAME, CODE))[1])]"/&gt;
      &lt;/xsl:copy&gt; 
    &lt;/xsl:template&gt;
    &lt;xsl:template match="ITEM"&gt;   
      &lt;xsl:copy&gt;      
          &lt;xsl:variable name="curr-group" select="key('itemkey', concat(NAME, CODE))" /&gt;  
         &lt;xsl:copy-of select="CODE|NAME"/&gt;        
          &lt;AMOUNT&gt;         
              &lt;xsl:value-of select="sum($curr-group/AMOUNT)"/&gt;        
          &lt;/AMOUNT&gt;       
      &lt;/xsl:copy&gt;         
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>It works perfect, but I have sum of all goods. But I only need sum if the CODE of delivery is SHIPPING122. I try to filter by test <code>CODE = 'SHIPPING107'</code>, but it doesnt work.</p>
  
  <p>Prefered XML output:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ITEM&gt;
      &lt;NAME&gt;Brambory 10 kg&lt;/NAME&gt;
      &lt;CODE&gt;4&lt;/CODE&gt;
      &lt;AMOUNT&gt;10&lt;/AMOUNT&gt;
  &lt;/ITEM&gt;
  &lt;ITEM&gt;
      &lt;NAME&gt;Česnek 1 kg&lt;/NAME&gt;
      &lt;CODE&gt;89&lt;/CODE&gt;
      &lt;AMOUNT&gt;3&lt;/AMOUNT&gt;
  &lt;/ITEM&gt;
  &lt;ITEM&gt;
      &lt;NAME&gt;Úterý - Liberec&lt;/NAME&gt;
      &lt;CODE&gt;SHIPPING122&lt;/CODE&gt;
      &lt;AMOUNT&gt;10&lt;/AMOUNT&gt;
      &lt;COUNT&gt;10&lt;/COUNT&gt;
  &lt;/ITEM&gt;
  </code></pre>
  
  <p>When is delivery place with <code>CODE = 'SHIPPING122'</code> I would like to sum goods Brambory, Česnek, etc. only from this delivery place, not from all orders (all delivery places).</p>