title: Using Saxon, increment or decrement the global variables in XSLT
body: <p>I want to use a counter to increment and decrement the value in XSLT. I am able to achieve this using Apache Xalan,  but now I want to achieve the same using Saxon.</p>
  
  <p>My XSLT script for Xalan looks like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:lxslt="http://xml.apache.org/xslt"
                  xmlns:result="http://www.example.com/results"
                  extension-element-prefixes="result"&gt;
  
      &lt;lxslt:component prefix="result" functions="incr dcr"&gt;
          &lt;lxslt:script lang="javascript"&gt;
              var count = 0;
              function incr() {
                count++;
                return count;
              }
  
              function dcr() {
                count--;
                return count;
              }
          &lt;/lxslt:script&gt;
      &lt;/lxslt:component&gt;
  
      &lt;xsl:template match="/"&gt;
          a)&lt;xsl:value-of select="result:incr()"/&gt;
          b)&lt;xsl:value-of select="result:incr()"/&gt;
          c)&lt;xsl:value-of select="result:dcr()"/&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Expected output is:</p>
  
  <pre class="lang-none prettyprint-override"><code>a)1
  b)2
  c)1
  
  ------------------------- my use case using saxon -------------------
  This is my sample data.xml file. I want to transform this to html file.
  
  &lt;entity&gt;
    &lt;record&gt;10&lt;/record&gt;
    &lt;record&gt;15&lt;/record&gt;
    &lt;record&gt;19&lt;/record&gt;
    &lt;record&gt;7&lt;/record&gt;
    &lt;record&gt;4&lt;/record&gt;
    &lt;record&gt;14&lt;/record&gt;
    &lt;record&gt;24&lt;/record&gt;
  &lt;entity&gt;
  
  I want to implement a counter for line-number and want to increment the counter and print the line-number
  my expected outputs:
  
   case 1: If record values &gt; 14, then my output should have line-number with value as.
   line-num  value    
    1        15
    2        19
    3        24
  
   case 2 : If record values &lt; 14, then my output should have line-number with value as.
   line-num  value    
    1         10
    2         7
    3         4
  
  My other use case :
  
  &lt;entity&gt;
    &lt;record&gt;10&lt;/record&gt;
    &lt;record&gt;15&lt;/record&gt;
    &lt;record&gt;19&lt;/record&gt;
    &lt;record&gt;7&lt;/record&gt;
    &lt;record&gt;20&lt;/record&gt;
    &lt;record&gt;14&lt;/record&gt;
    &lt;record&gt;24&lt;/record&gt;
      &lt;entity&gt;
         &lt;record&gt;30&lt;/record&gt;
         &lt;record&gt;3&lt;/record&gt;
      &lt;/entity&gt;
  &lt;/entity&gt;
  &lt;entity&gt;
    &lt;record&gt;5&lt;/record&gt;
    &lt;record&gt;17&lt;/record&gt;
    &lt;record&gt;19&lt;/record&gt;
    &lt;record&gt;6&lt;/record&gt;
    &lt;record&gt;70&lt;/record&gt;
    &lt;record&gt;9&lt;/record&gt;
    &lt;record&gt;35&lt;/record&gt;
      &lt;entity&gt;
         &lt;record&gt;15&lt;/record&gt;
         &lt;record&gt;2&lt;/record&gt;
      &lt;/entity&gt;
  &lt;/entity&gt;
  
  
  This is my other use case, first &lt;entity&gt; record value &gt; 15 and in second &lt;entity&gt; record value &lt; 10, and my &lt;entity&gt; can grow bigger where i have to show only some record based on condition.
  
   line-num  value 
      1        19 
      2        20  
      3        24
      4        30 
      5        5
      6        6
      7        2   
  </code></pre>