math.power.function.xsl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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="func math">
  7. <func:function name="math:power">
  8. <xsl:param name="base" select="0" />
  9. <xsl:param name="power" select="1" />
  10. <xsl:choose>
  11. <xsl:when test="$power &lt; 0 or contains(string($power), '.')">
  12. <xsl:message terminate="yes">
  13. The XSLT implementation of math:power() doesn't support
  14. negative or fractional arguments.
  15. </xsl:message>
  16. <func:result select="number('NaN')" />
  17. </xsl:when>
  18. <xsl:otherwise>
  19. <func:result select="math:_power($base, $power, 1)" />
  20. </xsl:otherwise>
  21. </xsl:choose>
  22. </func:function>
  23. <func:function name="math:_power">
  24. <xsl:param name="base" select="0" />
  25. <xsl:param name="power" select="1" />
  26. <xsl:param name="result" select="1" />
  27. <xsl:choose>
  28. <xsl:when test="$power = 0">
  29. <func:result select="$result" />
  30. </xsl:when>
  31. <xsl:otherwise>
  32. <func:result
  33. select="math:_power($base, $power - 1, $result * $base)" />
  34. </xsl:otherwise>
  35. </xsl:choose>
  36. </func:function>
  37. </xsl:stylesheet>