RE: [xsl] error:XSLT Stylesheet (possibly) contains a recursion.

Subject: RE: [xsl] error:XSLT Stylesheet (possibly) contains a recursion.
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 1 Dec 2007 15:41:05 -0000
It's a pretty ropey error message - your stylesheet clearly does contain a
recursion and there's nothing wrong with that.

>     <xsl:when test="substring($text,$length-1,$length)=' ' ">

I'll assume you either wrote "$length -1" with a space, or that you've got a
buggy XSLT processor that treated the "-" as a minus sign rather than a
hyphen.

If $text is "abcde", then this selects "de", which I don't think is what you
intended. Perhaps you're confusing it with Java's substring method.

> basically I check if the last char of the string is space, 

No, that's not what your code is doing. Check the spec.

Your recursive call does this:

<xsl:with-param name="text" select="substring($text,1,$length-1)"/>

again, I assume you must really have written "$length -1" otherwise this
wouldn't compile at all. If $length is greater than one, this will always
select a shorter string that you started with, which suggests that the
recursion ought to terminate. Unfortunately, though, when the string reaches
length zero, your test test="substring($text,$length-1,$length)=' ' ">
returns false, so you recurse infinitely processing a zero-length string.
Even if you correct your xsl:when to test properly for a trailing space,
it's obvious that your template will never terminate if passed a string that
is zero-length or that contains no spaces.

Michael Kay
http://www.saxonica.com/

Current Thread