sqrt.xsl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns:system_cache__appinfo="http://biuro.biall-net.pl/xmlschema_procesy5/default_db_xml_cache/appinfo.xsd"
  5. xmlns:math="http://exslt.org/math"
  6. exclude-result-prefixes="xs"
  7. version="2.0">
  8. <xsl:function name="math:sqrt">
  9. <xsl:param name="number"/>
  10. <xsl:call-template name="math:sqrt">
  11. <xsl:with-param name="number" select="$number"/>
  12. </xsl:call-template>
  13. </xsl:function>
  14. <xsl:template name="math:sqrt">
  15. <!-- The number you want to find the square root of -->
  16. <xsl:param name="number"
  17. select="0" />
  18. <!-- The current 'try'. This is used internally. -->
  19. <xsl:param name="try"
  20. select="1" />
  21. <!-- The current iteration, checked against maxiter to limit loop count -->
  22. <xsl:param name="iter"
  23. select="1" />
  24. <!-- Set this up to ensure against infinite loops -->
  25. <xsl:param name="maxiter"
  26. select="10" />
  27. <!-- This template was written by Nate Austin using Sir Isaac Newton's
  28. method of finding roots -->
  29. <xsl:choose>
  30. <xsl:when test="$try * $try = $number or $iter > $maxiter">
  31. <xsl:value-of select="$try" />
  32. </xsl:when>
  33. <xsl:otherwise>
  34. <xsl:call-template name="math:sqrt">
  35. <xsl:with-param name="number"
  36. select="$number" />
  37. <xsl:with-param name="try"
  38. select="$try - (($try * $try - $number) div (2 * $try))" />
  39. <xsl:with-param name="iter"
  40. select="$iter + 1" />
  41. <xsl:with-param name="maxiter"
  42. select="$maxiter" />
  43. </xsl:call-template>
  44. </xsl:otherwise>
  45. </xsl:choose>
  46. </xsl:template>
  47. </xsl:stylesheet>