Subject: Re: [xsl] [XSLT 1.0] How to get the last number in a string of numbers? From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 23 Dec 2009 10:05:12 -0500 |
I have a variable that contains a bunch of numbers:
<xsl:variable name="numbers"> generate some numbers </xsl:variable>
I want to obtain the last number in $numbers.
For example, this might be the value of $numbers:
6 23 45 67
I want the last number:
67
I want the last number fast, i.e., I want it in constant time, not O(n), where n is the length of the list of numbers.
Plus, I don't want to use any extension functions/elements.
Finally, I want to use only XSLT 1.0
Can it be done? How?
Last: 67 Last: 67 T:\ftemp>type roger.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="numbers"> 6 23 45 67 </xsl:variable>
<xsl:template match="/"> <xsl:call-template name="find-last-token-using-tokens"> <xsl:with-param name="nums" select="normalize-space($numbers)"/> </xsl:call-template>
<xsl:call-template name="find-last-token-using-string"> <xsl:with-param name="nums" select="normalize-space($numbers)"/> </xsl:call-template> </xsl:template>
<xsl:template name="find-last-token-using-tokens"> <xsl:param name="nums"/> <xsl:choose> <xsl:when test="contains($nums,' ')"> <xsl:call-template name="find-last-token-using-tokens"> <xsl:with-param name="nums" select="substring-after($nums,' ')"/> </xsl:call-template> </xsl:when> <xsl:otherwise> Last: <xsl:value-of select="$nums"/> </xsl:otherwise> </xsl:choose> </xsl:template>
<xsl:template name="find-last-token-using-string"> <xsl:param name="nums"/> <xsl:param name="last"/> <xsl:variable name="last-char" select="substring($nums,string-length($nums))"/> <xsl:choose> <xsl:when test="$last-char and $last-char!=' '"> <xsl:call-template name="find-last-token-using-string"> <xsl:with-param name="nums" select="substring($nums,1,string-length($nums)-1)"/> <xsl:with-param name="last" select="concat($last-char,$last)"/> </xsl:call-template> </xsl:when> <xsl:otherwise> Last: <xsl:value-of select="$last"/> </xsl:otherwise> </xsl:choose> </xsl:template>
-- UBL and Code List training: Copenhagen, Denmark 2010-02-08/10 XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19 XSLT/XQuery/XPath training: San Carlos, California 2010-04-26/30 Vote for your XML training: http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] [XSLT 1.0] How to get the las, Costello, Roger L. | Thread | Re: [xsl] [XSLT 1.0] How to get the, David Carlisle |
Re: [xsl] using xsl:output-characte, Tom T | Date | Re: [xsl] [XSLT 1.0] How to get the, David Carlisle |
Month |