math.sqrt.template.xsl 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  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. extension-element-prefixes="math">
  6. <xsl:template name="math:sqrt">
  7. <!-- The number you want to find the square root of -->
  8. <xsl:param name="number" select="0" />
  9. <!-- The current 'try'. This is used internally. -->
  10. <xsl:param name="try" select="1" />
  11. <!-- The current iteration, checked against maxiter to limit loop count -->
  12. <xsl:param name="iter" select="1" />
  13. <!-- Set this up to ensure against infinite loops -->
  14. <xsl:param name="maxiter" select="10" />
  15. <!-- This template was written by Nate Austin using Sir Isaac Newton's
  16. method of finding roots -->
  17. <xsl:choose>
  18. <xsl:when test="$try * $try = $number or $iter > $maxiter">
  19. <xsl:value-of select="$try"/>
  20. </xsl:when>
  21. <xsl:otherwise>
  22. <xsl:call-template name="sqrt">
  23. <xsl:with-param name="number" select="$number"/>
  24. <xsl:with-param name="try"
  25. select="$try - (($try * $try - $number) div (2 * $try))"/>
  26. <xsl:with-param name="iter" select="$iter + 1"/>
  27. <xsl:with-param name="maxiter" select="$maxiter"/>
  28. </xsl:call-template>
  29. </xsl:otherwise>
  30. </xsl:choose>
  31. </xsl:template>
  32. </xsl:stylesheet>