title: Arithmetic operation does not work when used in with-param in xsl
body: <p>Could someone please explain why the following does not work.</p>
  
  <p>Here is the xml:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;testsuites&gt;
      &lt;testsuite errors="0" failures="0" name="test_ui_orchestration" tests="10" time="90.190"/&gt;
      &lt;testsuite errors="1" failures="0" name="test_ui_tables" tests="13" time="1115.771"/&gt;
      &lt;testsuite errors="0" failures="3" name="test_ui_dashboard" tests="18" time="116.397"/&gt;
  &lt;/testsuites&gt;
  </code></pre>
  
  <p>I want to calculate total number of tests, total pass and fail.  I have a problem getting total number of failures (failures + errors) and total number of pass (for simplicity: tests - failures).  I call the same xml-template for calculating totals passing in different values using "with-param" as follows (section within "Calculate sums" comments):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:template match="/testsuites"&gt;
    &lt;html&gt;
    &lt;body&gt;
  
    &lt;!-- Calculate sums start --&gt;
    &lt;xsl:variable name="allSum"&gt;
      &lt;xsl:call-template name="testsSum"&gt;
        &lt;xsl:with-param name="numTests" select="testsuite/@tests"/&gt;
      &lt;/xsl:call-template&gt;
    &lt;/xsl:variable&gt;
      &lt;xsl:variable name="failedSum"&gt;
        &lt;xsl:call-template name="testsSum"&gt;
          &lt;xsl:with-param name="numTests" select="testsuite/@failures"/&gt;
        &lt;/xsl:call-template&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:variable name="errorSum"&gt;
        &lt;xsl:call-template name="testsSum"&gt;
          &lt;xsl:with-param name="numTests" select="testsuite/@errors"/&gt;
        &lt;/xsl:call-template&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:variable name="failederrorSum"&gt;
        &lt;xsl:call-template name="testsSum"&gt;
          &lt;xsl:with-param name="numTests" select="testsuite/@failures + testsuite/@errors"/&gt;
        &lt;/xsl:call-template&gt;
      &lt;/xsl:variable&gt;
    &lt;!-- Calculate sums ends --&gt;
  
    &lt;h2&gt;Test Results&lt;/h2&gt;
    &lt;table border="1"&gt;
      &lt;tr bgcolor="#9acd32"&gt;
        &lt;th&gt;Test Area&lt;/th&gt;
        &lt;th&gt;Total&lt;/th&gt;
        &lt;th&gt;Pass&lt;/th&gt;
        &lt;th&gt;Fail&lt;/th&gt;
        &lt;th&gt;Error&lt;/th&gt;
        &lt;th&gt;Fail and Error&lt;/th&gt;
      &lt;/tr&gt;
  
      &lt;xsl:for-each select="testsuite"&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;xsl:value-of select="@name"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;xsl:value-of select="@tests"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;xsl:value-of select="@tests - (@failures + @errors)"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;xsl:value-of select="@failures"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;xsl:value-of select="@errors"/&gt;&lt;/td&gt;
                &lt;td&gt;&lt;xsl:value-of select="@failures + @errors"/&gt;&lt;/td&gt;
                &lt;td&gt; &lt;/td&gt;
            &lt;/tr&gt;
      &lt;/xsl:for-each&gt;
  
      &lt;tr&gt;
        &lt;td&gt;&lt;b&gt;All Tests&lt;/b&gt;&lt;/td&gt;
        &lt;td&gt;&lt;xsl:value-of select="$allSum"/&gt;&lt;/td&gt;
        &lt;td&gt;foo&lt;/td&gt;
          &lt;!--
        &lt;td&gt;&lt;xsl:value-of select="$passedSum"/&gt;&lt;/td&gt;
        --&gt;
        &lt;td&gt;&lt;xsl:value-of select="$failedSum"/&gt;&lt;/td&gt;
        &lt;td&gt;&lt;xsl:value-of select="$errorSum"/&gt;&lt;/td&gt;
        &lt;td&gt;&lt;xsl:value-of select="$failederrorSum"/&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
    &lt;/table&gt;
    &lt;/body&gt;
    &lt;/html&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template name="testsSum"&gt;
    &lt;xsl:param name="numTests"/&gt;
      &lt;xsl:choose&gt;
        &lt;xsl:when test="$numTests"&gt;
          &lt;xsl:variable name="recursive_result"&gt;
            &lt;xsl:call-template name="testsSum"&gt;
              &lt;xsl:with-param name="numTests" select="$numTests[position() &gt; 1]"/&gt;
            &lt;/xsl:call-template&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:value-of select="number($numTests[1]) + $recursive_result"/&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;&lt;xsl:value-of select="0"/&gt;&lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>This produces the following output for the sums (full output is at the end of this post):</p>
  
  <pre><code>41 3 1 0
  </code></pre>
  
  <p>while it should be:</p>
  
  <pre><code>41 3 1 4
  </code></pre>
  
  <p>Each individual sum works fine, the corresponding attribute is found.  However, when I attempt to add two attributes and pass that to the sum template, it does not work as expected.</p>
  
  <p>It gets worse.  The code for generating total passed tests is:</p>
  
  <pre><code>&lt;xsl:variable name="passedSum"&gt;
      &lt;xsl:call-template name="testsSum"&gt;
          &lt;xsl:with-param name="numTests"
                  select="testsuite/@tests - testsuite/@failures"/&gt;
      &lt;/xsl:call-template&gt;
  &lt;/xsl:variable&gt;
  </code></pre>
  
  <p>When the above code is enabled, I get the following error:</p>
  
  <pre><code>Failed to evaluate the expression of variable 'numTests'
  </code></pre>
  
  <p>I've been searching online and understand that the "select" of "with-param" accepts "An XPath expression that defines the value of the parameter" (from w3schools).  An XPath expression can contain arithmetic operations (<a href="http://www.w3schools.com/xpath/xpath_operators.asp" rel="nofollow">http://www.w3schools.com/xpath/xpath_operators.asp</a>), so why do the above fail?</p>
  
  <p>Here is full output of the xsl transformation:</p>
  
  <pre>
  Test Area               Total   Pass    Fail    Error   Fail and Error
  ----------------------------------------------------------------------
  test_ui_orchestration   10      10      0       0       0
  test_ui_tables          13      12      0       1       1   
  test_ui_dashboard       18      15      3       0       3
  ----------------------------------------------------------------------  
  All Tests               41      foo     3       1       0
  </pre>
  
  <p>Here is the <strong>expected</strong> output of the xsl transformation (modified values marked with *):</p>
  
  <pre>
  Test Area               Total   Pass    Fail    Error   Fail and Error
  ----------------------------------------------------------------------
  test_ui_orchestration   10      10      0       0       0
  test_ui_tables          13      12      0       1       1   
  test_ui_dashboard       18      15      3       0       3
  ----------------------------------------------------------------------  
  All Tests               41      37*     3       1       4*
  </pre>