<?xml version="1.0" encoding="UTF-8"?>
<!--
generates a square root of a value using babylonian method.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="root">
<out>
<xsl:for-each select="value">
<xsl:call-template name="approx-root">
<xsl:with-param name="value" select="."/>
<xsl:with-param name="guess" select="number(.) div 2"/>
</xsl:call-template>
</xsl:for-each>
</out>
</xsl:template>
<xsl:template name="approx-root">
<xsl:param name="value"/>
<xsl:param name="guess"/>
<xsl:param name="level">10</xsl:param>
<xsl:if test="$level = 0">
<xsl:value-of select="$guess"/>
</xsl:if>
<xsl:if test="$level > 0">
<xsl:call-template name="approx-root">
<xsl:with-param name="guess" select="($guess + $value div $guess) div 2"/>
<xsl:with-param name="value" select="$value"/>
<xsl:with-param name="level" select="$level - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
No comments:
Post a Comment