math.sqrt.function.xsl 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. xmlns:math="http://exslt.org/math"
  5. xmlns:func="http://exslt.org/functions"
  6. extension-element-prefixes="math func">
  7. <func:function name="math:sqrt">
  8. <!-- The number you want to find the square root of -->
  9. <xsl:param name="number" select="0" />
  10. <!-- The current 'try'. This is used internally. -->
  11. <xsl:param name="try" select="1" />
  12. <!-- The current iteration, checked against maxiter to limit loop count -->
  13. <xsl:param name="iter" select="1" />
  14. <!-- Set this up to ensure against infinite loops -->
  15. <xsl:param name="maxiter" select="10" />
  16. <!-- This template was written by Nate Austin using Sir Isaac Newton's
  17. method of finding roots -->
  18. <xsl:choose>
  19. <xsl:when test="$try * $try = $number or $iter > $maxiter">
  20. <func:result select="$try"/>
  21. </xsl:when>
  22. <xsl:otherwise>
  23. <func:result select="math:sqrt($number,
  24. $try - (($try * $try - $number) div (2 * $try)),
  25. $iter + 1, $maxiter)" />
  26. </xsl:otherwise>
  27. </xsl:choose>
  28. </func:function>
  29. </xsl:stylesheet>