Re: [xsl] collapsing number ranges

Subject: Re: [xsl] collapsing number ranges
From: J-P S <jps@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 27 Aug 2004 10:32:41 +0100 (BST)
On Fri, 27 Aug 2004, Jeni Tennison wrote:

) > 7777-7778 => 7777-78
)
) Taking a guess at the algorithm, what about:
)
)   <xsl:value-of select="@begin" />
)   <xsl:text>-</xsl:text>
)   <xsl:choose>

Gah! Of course! xsl:choose, but with recursion. I'm always averse to
recursive template calls, but I think the below will work for the general
case. Call this template with your start and end numbers, and "0" for the
digit parameter (or "1" or "2" if you *know* the first one or two digits
will always be the same, to speed things up):

	<xsl:template name="number">

	  <xsl:param name="start" />
	  <xsl:param name="end"/>
	  <xsl:param name="digits"/>

	  <xsl:choose>

	    <!-- If strings are the same for first $digit digits, call
		self with $digit+1 and test again -->
	    <xsl:when test="substring($start,1,$digits) =
			substring($end,1,$digits)">
	      <xsl:call-template name="number">
	        <xsl:with-param name="start" select="$start"/>
	        <xsl:with-param name="end" select="$end"/>
	        <xsl:with-param name="digits" select="$digits+1"/>
	      </xsl:call-template>
	    </xsl:when>

	    <!-- If strings aren't the same, then print $start,
		followed by a dash, followed by the tail end of $end -->
	    <xsl:otherwise>
	      <xsl:value-of select="concat($start,'-',
			substring($end,$digits,string-length($end)))"/>
	    </xsl:otherwise>

	  </xsl:choose>

	</xsl:template>

Let me know how you get on!

Cheers,
J-P
-- 
(First Beep is cabron, meaning your wife is unfaithful.  Second Beep is
maricon, which means faggot. It is very simple to make out what he is
saying because the beeps do not delete the words completely and or in
combination with his lip movements you make out the words.)

Current Thread