末尾最適化はされてない

合計を求めるXSLTに、30000渡したら、

XSL プロセッサ スタックがオバーフローしています。無限のテンプレート再帰が原因となった可能性があります。

だってさ。
以下XSLT本体。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                <title>sum</title>
            </head>
            <body>
                <p>
                    <xsl:apply-templates select="sum"/>
                </p>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="sum">
        <xsl:call-template name="sum-impl">
            <xsl:with-param name="n" select="@n"/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="sum-impl">
        <xsl:param name="n"/>
        <xsl:param name="r" select="0"/>
        <xsl:if test="$n=0">
            <xsl:value-of select="$r"/>
        </xsl:if>
        <xsl:if test="$n!=0">
            <xsl:call-template name="sum-impl">
                <xsl:with-param name="n" select="$n - 1"/>
                <xsl:with-param name="r" select="$n + $r"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>