XSLT and Mathematics
I've been studying XSLT lately. After I found out that it can produce factorials, I decided to devise an xsl file that could produce a hailstorm sequence:
<?xml version="1.0"?>You can start by naming the above file "hailstor.xsl" and running it on the following:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements='*'/>
<xsl:template name="hailstorm">
<xsl:param name="n" select="1"/>
<xsl:variable name="sequence">
<xsl:if test="$n mod 2 = 0">
<xsl:call-template name="hailstorm">
<xsl:with-param name="n" select="$n div 2"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="($n mod 2) * ($n - 1) > 0">
<xsl:call-template name="hailstorm">
<xsl:with-param name="n" select="$n * 3 + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:variable>
<xsl:value-of select="$n"/>
<xsl:if test="$n > 1">
<xsl:value-of select="', '"/>
</xsl:if>
<xsl:value-of select="$sequence"/>
</xsl:template>
<xsl:template match="number">
<xsl:variable name="x" select="."/>
<xsl:variable name="y">
<xsl:call-template name="hailstorm">
<xsl:with-param name="n" select="$x"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="'hailstorm('"/>
<xsl:value-of select="$x"/>
<xsl:value-of select="') = '"/>
<xsl:value-of select="$y"/>
<xsl:value-of select="'
'"/>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<?xml-stylesheet href="hailstor.xsl" type="text/xsl"?>
<num>27</num>
0 Comments:
Post a Comment
<< Home