math.power.template.xsl 1.5 KB

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